6

有没有人有一个脚本来列出 SQL Server 数据库中所有现有索引的 CREATE INDEX 语句?

SQL Server DB 中所有索引和索引列的这个线程列表对如何找到它们有很好的提示。但是生成CREATE INDEX语句的脚本会很棒。有时我们会遇到没有足够数据的情况,或者随着时间的推移在没有文档的情况下以临时方式添加了索引,因此缺少 create 语句。就像我现在所处的情况一样。

谢谢。

4

4 回答 4

13

使用从 SQL Management Studio 生成脚本并选择“脚本索引”选项(在高级脚本选项下)

于 2012-10-01T11:03:17.177 回答
4

前段时间我为此写了一些东西。您可能必须根据需要对其进行修改,但至少您有一个骨架。

if exists (select 1 from information_schema.routines where routine_name = 'Script_CreateIndex')
    drop proc Script_CreateIndex
go

create proc Script_CreateIndex (
    @TableName varchar(124)
)
as
begin
    if not exists (select 1 from sys.indexes where object_name(object_id) = @TableName and type_desc in ('CLUSTERED', 'NONCLUSTERED'))
        return

    declare @IndexList table (
        Id int identity,
        IndexName varchar(124),
        IndexDescription varchar(max),
        IndexKeys varchar(max)
    )

    insert @IndexList(IndexName, IndexDescription, IndexKeys)
        exec sp_helpindex @TableName

    if (select count(*) from @IndexList) > 0
    begin
        select '-- Creating indexes for table ' + @TableName

        while exists (select 1 from @IndexList) 
        begin
            declare @Id int, @IndexName varchar(124), @IndexDescription varchar(max), @IndexKeys varchar(max)
            select top 1 @Id = Id, @IndexName = IndexName, @IndexDescription = IndexDescription, @IndexKeys = IndexKeys from @IndexList order by Id
            delete from @IndexList where Id = @Id

            declare @Clustered varchar(10), @Unique varchar(7)

            select @Clustered = case when patindex('%nonclustered%', @IndexDescription) > 0 then '' else ' clustered ' end
            select @Unique = case when patindex('%unique%', @IndexDescription) > 0 then ' unique ' else '' end

            select 'if not exists (select 1 from sys.indexes where name = ''' + @IndexName + ''')'
            select 'begin'
            select char(9) + 'create' + @Unique + @Clustered + ' index [' + @IndexName + '] on [' + @TableName + '](' + @IndexKeys + ')'
            select char(9) + 'select ''Index ' + @IndexName + ' created.'''
            select 'end'
            select 'go'
        end

        select ''
        select ''
    end
end
go

grant exec on Script_CreateIndex to public
select 'Script_CreateIndex compiled.' 'Job'
go
于 2012-10-01T11:11:18.367 回答
4

在这里查看我的解决方案: https ://stackoverflow.com/a/55742250/1831734

输出

Create                                                                                                      Drop                                    Rebuild
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE CLUSTERED INDEX [PK_Table1] ON [Table1] ( [Tab1_ID] )                                                DROP INDEX [PK_Table1] ON [Table1]      ALTER INDEX [PK_Table1] ON [Table1] REBUILD 
CREATE UNIQUE INDEX [IX_Table1_Name] ON [Table1] ( [Tab1_Name] )                                            DROP INDEX [IX_Table1_Name] ON [Table1] ALTER INDEX [IX_Table1_Name] ON [Table1] REBUILD 
CREATE NONCLUSTERED INDEX [IX_Table2] ON [Table2] ( [Tab2_Name], [Tab2_City] )  INCLUDE ( [Tab2_PhoneNo] )  DROP INDEX [IX_Table2] ON [Table2]      ALTER INDEX [IX_Table2] ON [Table2] REBUILD
于 2019-04-18T08:49:04.973 回答
1

您可以使用“对象资源管理器”窗口逐个表执行此操作

转到 Management Studio 中的 Indexes 文件夹,突出显示该文件夹,然后打开 Object Explorer 窗格

然后,您可以“移位选择”该表上的所有索引,如果您右键单击脚本“CREATE TO”,它将为您创建一个包含所有相关索引的脚本。

于 2012-10-01T11:12:19.230 回答