Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I bet with SMO you can do this no problem:</p> <ul> <li>read the structure of your "old" table into in-memory variables</li> <li>use this structure info to create the new table</li> </ul> <p>I quickly found a few interesting articles that show at least part of the solution:</p> <ul> <li><a href="http://www.sqlservercentral.com/articles/SMO/scriptdatabaseobjectswithsmo/2342/" rel="nofollow noreferrer">http://www.sqlservercentral.com/articles/SMO/scriptdatabaseobjectswithsmo/2342/</a></li> </ul> <p>So basically it would boil down to something like this:</p> <pre><code>Server localServer = new Server("(local)"); Database testDB = localServer.Databases["test"]; Table myTable = testDB.Tables["TestFiles"]; myTable.Refresh(); Table newTable = new Table(testDB, "MyNewTableName"); foreach(Column col in myTable.Columns) { Column newColumn = new Column(newTable, col.Name); newColumn.DataType = col.DataType; newColumn.Default = col.Default; newColumn.Identity = col.Identity; newColumn.IdentityIncrement = col.IdentityIncrement; newColumn.IdentitySeed = col.IdentitySeed; newColumn.Nullable = col.Nullable; newTable.Columns.Add(newColumn); } newTable.Create(); </code></pre> <p>Of course, there are more properties on the "Column" which you might want to copy over, plus you might also want to copy indices, constraints etc. - extra work.</p> <p>I'm stumped that there isn't an easier way to duplicate a "Column" object to a new one (something like a .Clone() method) to ease this - maybe it's not a top-priority scenario, I don't know....</p> <p>Hope this helps!</p> <p>Marc</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload