Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying table structure 1

Status
Not open for further replies.

kevinluebb

Programmer
Jan 4, 2002
30
US
Is it possible to copy a table structure? I have created one table that I would like to copy since it does contain most of the rows that I need (I'll zap the ones I don't).
 
One of the solutions could be
First generate SQL script of the original table through SQL server enterprise manager.
right click on the oroginal table name,
Clock on All Task: Generate script.
Save this script to a file.
Replace the original table name by the new table name and run the script through SQL query analyzer. This will create the replica of the orginal table structure and then execute

insert into newtable
select * from orignaltable

will insert the records from the original table to the new one.

I hope it helps you.

 
You can use a SELECT INTO to create a table with all, some or none of the rows from the original table.

--Create new table with all rows from the old table
Select * Into NewTable From OldTable

--Create new table with some rows from the old table
Select * Into NewTable From OldTable
Where <define the selection criteria>

--Create new table with no rows from the old table
Select * Into NewTable From OldTable
Where 1=2

NOTE 1: 1=2 always evaluates to false so no rows are selected but the table structure wiull be duplicated.

NOTE 2: This method will not create indexes, defaults, primary keys, etc so scripting is a better choice. I simply offer this as alternative which is often useful in stored procedures. Terry L. Broadbent
Programming and Computing Resources
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top