Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically copy a table in SQL Server, without the data?

I need to copy a table I already I have in a SQL server database, but I don't need the data contained in the source table. The examples I found only involve copying the data.

Here are the details:

  1. Copy the table structure only, not the data.
  2. Source and target tables are in the same database.
  3. Target table does not exist yet.
  4. Need to do this programmatically
  5. It would be nice to have any related properties of the table/columns be copied as well.
like image 602
John B Avatar asked Jul 31 '09 20:07

John B


2 Answers

select * into new_table from old_table where 1=0

Execute the above command programmatically.

like image 67
msvcyc Avatar answered Oct 24 '22 12:10

msvcyc


Script out the table, change the table name in the script, run the script.

like image 32
MusiGenesis Avatar answered Oct 24 '22 13:10

MusiGenesis