Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 copy table structure without data

Author  Topic 

landau66
Yak Posting Veteran

61 Posts

Posted - 2008-07-10 : 04:31:31
hello everyone!

i have a table. since i want to copy the structure of the table (name of columns and datatypes) but dont want to have the data itself, i want to ask if there is function that facilitates this.

many thankx in advance and greetings from vienna

landau66

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2008-07-10 : 04:33:58
No, you can right click on the table and script the table out or on the database level go to Tasks -> generate script...

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-10 : 04:34:45
Script out table and run it in your db or use

SELECT TOP 0 * INTO NewTable FROM OldTable
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-07-10 : 05:26:15
Something to be aware of...

Scripting the table will also give you the keys and constraints if you need them (though you can edit them out).

A SELECT TOP 0 * will only give you the column names and data types.

-------------
Charlie
Go to Top of Page

ranganath
Posting Yak Master

209 Posts

Posted - 2008-07-10 : 05:43:59
Hi,

Try with this

Select * into SomeTableName From Table where 1 = 2
Go to Top of Page

EugeneLim11
Posting Yak Master

167 Posts

Posted - 2008-07-10 : 06:09:47
ranganath, since 1 <> 2, nothing goes into the table but the table structure is there. Brillant!

But there is no preservation of all keys (foreign keys), constraints etc. Also, the SomeTableName needs to be specified (now why do you want to duplicate a table in the same database? )

Scripting the table is a much better way, in my humble opinion. :) You will get all code for keys, constraints etc and can export the sql out to another database (which I assume is what landau66 wants to do).
Go to Top of Page

mikedakins
Starting Member

2 Posts

Posted - 2008-10-06 : 15:56:36
Hello.

I have the same problem. I want to copy a template table with a script which maintains keys and constraints. The template may change without my knowledge but my script must still work. Can I script the table based on the structure of another table (ie get another tables properties first). Also I need it to be database type independant so that it works on any standard ODBC database.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-06 : 23:59:24
quote:
Originally posted by mikedakins

Hello.

I have the same problem. I want to copy a template table with a script which maintains keys and constraints. The template may change without my knowledge but my script must still work. Can I script the table based on the structure of another table (ie get another tables properties first). Also I need it to be database type independant so that it works on any standard ODBC database.


to main keys and constraints, best way is to script out table and then apply to new db as mentioned above.
Go to Top of Page
   

- Advertisement -