title: Programmatically copy indexes from one table to another in SQL Server
body: <p>Basically the exact same question as in this question: <a href="https://stackoverflow.com/questions/652148/how-to-copy-indexes-from-one-table-to-another-in-sqlserver">How to copy indexes from one table to another in SQL Server</a>, BUT, how do I do it programmatically in T-SQL, given a source table name and destination table name? </p>
  
  <p>I.e. without knowing what table up front.</p>
  
  <p>I can copy the basic structure</p>
  
  <pre><code>SELECT TOP (0) * INTO [BackupTable] FROM [OriginalTable]
  </code></pre>
  
  <p>But that doesn't copy indexes, constraints, triggers etc</p>
  
  <p>I ideally would like a stored proc that looks something like:</p>
  
  <pre><code>spCloneTableStructure @ExistingTableName, @NewTableName
  </code></pre>
  
  <p>That copies the columns, primary keys and indexes</p>
  
  <p>Anything like that exist? (note that I'm on SQL Server 2008 R2)</p>