【问题标题】:Generate CREATE INDEX statements in SQL Server在 SQL Server 中生成 CREATE INDEX 语句
【发布时间】:2012-09-22 05:27:18
【问题描述】:

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

此线程List of all index & index columns in SQL Server DB 提供了有关如何找到它们的重要提示。但是生成CREATE INDEX 语句的脚本会很棒。有时我们会遇到没有足够数据的情况,或者随着时间的推移在没有文档的情况下以临时方式添加了索引,因此缺少 create 语句。就像我现在所处的情况一样。

谢谢。

【问题讨论】:

标签: sql-server indexing


【解决方案1】:

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

【讨论】:

  • 这非常有效。谢谢! (也感谢其他人,感谢您的帮助)。
  • 我可能说的很明显,但是要到达那里,您可以右键单击数据库 -> 任务 -> 生成脚本...
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    在此处查看我的解决方案: 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
    

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-06
        • 1970-01-01
        • 1970-01-01
        • 2022-12-02
        • 1970-01-01
        • 2019-05-05
        • 2013-09-26
        相关资源
        最近更新 更多