views:

57

answers:

3

I am trying to create a new table by copying an existing table in SQL Server 2008 using Management Studio. The existing table contains no data. I am using the following code but am receiving an error for Incorrect Syntax near AS. I am not sure what is wrong here. I am a SQL newb and any help would be appreciated. Thanks.

CREATE TABLE Drop_Centers_Detail
    AS (Select * From Centers_Detail)
+4  A: 

like this, however this will not create indexes and constraints

select * into Drop_Centers_Detail
from Centers_Detail
where 1 = 0
SQLMenace
Supporting documentation - INTO clause: http://msdn.microsoft.com/en-us/library/ms188029.aspx
OMG Ponies
be sure to read the "Limitations and Restrictions" in the link given by @OMG Ponies. You won't get computed columns, indexes, constraints, or triggers from the source table.
KM
Thanks for the answer and the info. It worked. Just out of curiosity is there a way to copy an existing table and preserve its indexes, constraints, etc.?
you will need to script it out in that case or you can use SMO (http://msdn.microsoft.com/en-us/library/ms162169.aspx)
SQLMenace
+1  A: 

In Sql Server Managment Studio, right-click your existing table and select Script Table as > Create to > New Query Editor Window. This will give you a better starting script that you can use as the base for your new schema.

SethO
Thanks for the info.
A: 

1) I would suggest generating a create script from the table you want to copy, and then run that on your destination database.

2) Write a Insert statement in another SQL Query window to import that data

Insert Into Database1.Table1(Field1, Field2) Select Field1, Field2 From Database2.Table

dretzlaff17