title: Generate T-SQL for Existing Indexes
body: <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>How would you do this programmatically (ideally through Python)?</p>