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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 copy table

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2005-09-07 : 05:48:51
is there a simple sql syntax to copy a table compete with data???

HCLollo
Starting Member

49 Posts

Posted - 2005-09-07 : 05:57:29
esthera, you mean something like the following?


INSERT INTO NewTable
SELECT *
FROM OldTable


HCL


"If it works fine, then it doesn't have enough features"
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2005-09-07 : 06:03:00
yes -- will that work if the newtable wasn't yet created or do I have to create it first?
Will it keep primary keys and identities and defaults?
Go to Top of Page

asarak
Starting Member

36 Posts

Posted - 2005-09-07 : 06:14:40
select * into newtable from oldtable

Go to Top of Page

HCLollo
Starting Member

49 Posts

Posted - 2005-09-07 : 06:31:48
Esthera, you should create the table before the INSERT.

HCL


"If it works fine, then it doesn't have enough features"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-09-07 : 07:01:37
"Will it keep primary keys and identities and defaults?"

Nope ...

SELECT * INTO TableToBeCreated FROM MyExistingTable

will create a new table with appropriate datatype for each column.

Otherwise you need to do

CREATE TABLE MyNewTable
(
...
)

INSERT INTO MyNewTable
SELECT *
FROM MyOldTable

SQL Server has a number of ways that you can use to get a "script" of the existing table, and then you can just "find&replace" to make a new script. This can include all indexes, PKs, defaults, IDENTITY, extended properties and so on

Kristen
Go to Top of Page
   

- Advertisement -