Generate T-SQL for Existing Indexes

Posted by Chris S on Stack Overflow See other posts from Stack Overflow or by Chris S
Published on 2010-06-10T20:57:49Z Indexed on 2010/06/10 21:03 UTC
Read the original article Hit count: 197

Filed under:
|

How do you programmatically generate T-SQL CREATE statements for existing indexes in a database? SQL Studio provides a "Script Index as->Create to" command that generates code in the form:

IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = N'IX_myindex')
    CREATE NONCLUSTERED INDEX [IX_myindex] ON [dbo].[mytable] 
    (
        [my_id] ASC
    )WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
GO

How would you do this programmatically (ideally through Python)?

© Stack Overflow or respective owner

Related posts about sql

Related posts about tsql