tags:

views:

220

answers:

3

how to create new table which structure should be same as another table

i tried, Create table dom as select * from dom1 where 1=2 but its not working error occured

+5  A: 

Try:

Select * Into <DestinationTableName> From <SourceTableName> Where 1 = 2

Note that this will not copy indexes, keys, etc.

If you want to copy the entire structure, you need to generate a Create Script of the table. You can use that script to create a new table with the same structure. You can then also dump the data into the new table if you need to.

If you are using Enterprise Manager, just right-click the table and select copy to generate a Create Script.

Kevin Crowell
Kevin, just a small formatting change in your answer:-Select * Into <DestinationTableName> From <SourceTableName> Where 1 = 2
ydobonmai
@Ashish Fixed, thanks.
Kevin Crowell
why to add where clause here 1=2 ???
BreakHead
Qutbuddin, 1=2 will prevent data copying from source to destination table. Try yourself:- CREATE TABLE Table1 ( Id int , Name varchar(200) ) INSERT INTO table1 VALUES (1,'A') INSERT INTO table1 VALUES(2,'B') -- Will create table2 with data in table1 SELECT * INTO Table2 FROM Table1 WHERE 1=2 -- Will create table2 without data in table1 SELECT * INTO Table2 FROM Table1 WHERE 1=2
ydobonmai
The SQL scripts in the comment do not appear correctly. :-)
ydobonmai
+1  A: 
SELECT * 
INTO NewTable
FROM OldTable
WHERE 1 = 2
Chris Latta
+1  A: 

I don't know why you want to do that, but try:

SELECT *
INTO NewTable
FROM OldTable
WHERE 1 = 2

It should work.

Adrian Faciu
I think that would also copy the data? he wants only the structure.
ydobonmai
@Ashis Gupta - Thanks, i've forgot the "where" :)
Adrian Faciu