[wish] "CREATE TABLE ... LIKE" in SQL Server 2005

My friend Dave recently told me about the "CREATE TABLE ... LIKE " SQL statement. MySql and DB2 both support it, and it's apparently part of the SQL99 spec. It's not supported by T-SQL (SQL Server).

CREATE TABLE LIKE is quick way of cloning the structure of an existing table, including any indexes (but not foreign keys). This can be particularly useful in creating new tables to be added to an existing MERGE table.

mysql> CREATE TABLE log_20041124 LIKE log_20041123;

As far as I know, this will not be supported by T-SQL, despite all the other SQL enhancements in Yukon. You can copy everything except the keys / indexes with "SELECT INTO":
 
select * into CustomersCopy from Customers where 1 = 0
 
That copies the structure, but doesn't copy the indexes.

One other trick I learned a bit ago: you can select a table in Enterprise Manager and hit control - c. Then go to notepad / query analyzer / etc. and paste to get the create table statement (with all the indexes and stuff). You can also right click a table in Query Analyzer and select "Script Object To New Window As..." to get a Create statement. Still, none of these offers quite what the CREATE TABLE ... LIKE statement does - script level copy of table scructure and indexes.

Published Sunday, January 30, 2005 7:37 AM by Jon Galloway
Filed under:

Comments

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

Another one I like from Informix land is when creating a stored procedure, you can specify the type of a parameter to be LIKE a column of a table.

Sunday, January 30, 2005 7:40 AM by Adam Weigert

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

How bout ..

sp_exec(sp_helptext(TABLENAME)) -- ;-)

Sunday, January 30, 2005 4:05 PM by Sahil Malik

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

And that Copy Paste stuff u mentioned, doesn't work for Sql2k5 :(

Sunday, January 30, 2005 4:06 PM by Sahil Malik

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

Adam -
That sounds even better. What's the syntax?

Sahil -
sp_exec(sp_helptext(tablename)) would try to create the table with the same name again, which would just cause an error since the table already exists, right? CREATE TABLE LIKE lets you specify a new table name.

Sunday, January 30, 2005 5:50 PM by Jon Galloway

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

You can do this to create a table with the same columns of another table.

SELECT TOP 0 * INTO #mytable FROM anotherTable


I can't think of a way to include indexes though :(

Monday, January 31, 2005 4:54 AM by Rhys Jeremiah

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

In SQL 2005 Server Management
Right Click on the table
Script Table as
Create to
File or Clipboard

Friday, June 16, 2006 10:55 PM by John Argast

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

John-
Scripting a table has been around in SQL Server (both Enterprise Mangager and Query Analyzer) since SQL Server 2000. That's great for one-off changes, but you can't include it in automated scripts or repeatable processes. A SQL Script allows that.

Saturday, June 17, 2006 2:47 AM by Jon Galloway

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

I wish to create a table named on the fly, specifically a snapshot of an inventory table on a certain date.

Using a variable..

"CREATE TABLE @NewTableName" does not seem to work in SQL2K

Help

Thursday, September 21, 2006 2:11 PM by Alan Monteath

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

@Alan - Table variables are available in SQL2K. You use a different syntax for them, though. Rather than CREATE TABLE you use DECLARE @tablename TABLE:

DECLARE @NewTableName TABLE

(

 ID int,

 Sample int

)

More info here: http://www.odetocode.com/Articles/365.aspx

Thursday, September 21, 2006 5:43 PM by Jon Galloway

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

hi,

i want to create a table such that itz should have the table name as sydate.every day table created with the current date..any1 ving idea about this

Tuesday, November 07, 2006 1:24 AM by neethu

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

Don't know if this helps but it creates a table on the fly:

declare @sql nvarchar(1000)

declare @tablename varchar(100)

set @tablename = 'TestTable'

set @sql = 'CREATE TABLE ' + @tablename + '(

   Name varchar(100),

   Age int

)'

exec sp_executesql @sql

Jan

Tuesday, November 21, 2006 1:29 AM by Jan

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

i want to create all the tables of a database with different databasename in Sql Server 2005

Saturday, September 01, 2007 5:05 AM by Ramesh

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

Select

*

Into  <NewtableName> from <OldTable> where <ColumnName>=-300

Thursday, September 13, 2007 3:01 AM by Sandeep

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

Select * Into #TEMPO from klients    

Wednesday, November 07, 2007 10:22 AM by sdsd

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

I am having real trouble created a table in an exisiting database on the fly.....

the application i am created is a kinda of staff rotaing system, and i need to save each days rota in its own table.

all i basically need to do is name the table with the date...can this be done?

Wednesday, January 30, 2008 5:19 AM by andrew

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

Really I felt very happy with instant duplication of my existing table.  Thank you very much.

Wednesday, July 09, 2008 5:01 PM by SURI

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

i got the answer thank u

Monday, August 18, 2008 2:44 AM by chaithanya

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

hey y've defined the lot of command. that's good to see and implement

Friday, October 03, 2008 7:06 AM by vinod

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

Ok but how to create table with inserting query that returns 10000 rows

Sunday, October 26, 2008 6:36 AM by Saket Bharti

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

To bad Microsoft won't just add SQL99 functionality (although in their defense I've seen some of the SQL99 and what they have added is pretty broad... I guess once it gets turned over to the ISO it all gets vague)

Tuesday, January 20, 2009 2:46 PM by meregistered

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

thx Jan - your example works perfectyly ;)

Monday, February 02, 2009 4:17 PM by jacek

# CREATE TABLE LIKE &#8230; in MS SQL Server &laquo; Prutsoft

Pingback from  CREATE TABLE LIKE &#8230; in MS SQL Server &laquo;  Prutsoft

Sunday, April 19, 2009 7:19 AM by CREATE TABLE LIKE … in MS SQL Server « Prutsoft

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

Found nothing helpful!

Friday, May 29, 2009 8:30 AM by Vilas Patil

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

Dear Friends,

Below SQL Server Query can help you to create a table form another one, without inserting any data. New table will have 0 rows still structure will be same as the Parent table.

select * into test2 from tbltmpCostCenter where 1=2

I Don’t this this is a Feasible one, Let me find it out for you.

Wednesday, June 17, 2009 8:36 AM by JOBY CHERIYAN [BANGALORE]

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

but it does not work with xml datatype in linked server

Thursday, July 02, 2009 2:21 AM by maninder

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

This is now way to crate a tables in sql..

Wednesday, August 12, 2009 12:17 AM by Krishna

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

To create a table on the fly with the date appended to the table name.

DECLARE @SourceTableName NVARCHAR(255)

DECLARE @TargetTableName NVARCHAR(255)

DECLARE @Date NVARCHAR(8)

DECLARE @NSQL NVARCHAR(1000)

SET @Date = '20090323'

SET @SourceTableName = 'dbo.SourceTableName'

SET @TargetTableName = 'dbo.TargetTableName' + '_' + @Date

SET @NSQL = 'SELECT *'

SET @NSQL = @NSQL + ' INTO ' + @TargetTableName

SET @NSQL = @NSQL + ' FROM ' + @SourceTableName + ' WHERE 1 = 0'

EXECUTE sp_executesql @NSQL

Tuesday, August 25, 2009 7:56 AM by Photolab

# re: [wish] "CREATE TABLE ... LIKE" in SQL Server 2005

This will not copy primary key and foreign key related information

Is there any way to copy all schema of table

Friday, October 09, 2009 6:41 AM by vikram

Leave a Comment

(required) 
(required) 
(optional)
(required)