tags:

views:

873

answers:

5

Hello

Basically I have a two databases on SQL Server 2005.

I want to take the table data from one database and copy it to another database's table.

I tried this:

SELECT * INTO dbo.DB1.TempTable FROM dbo.DB2.TempTable

This didn't work.

I don't want to use a restore to avoid data loss...

Any ideas?

+5  A: 

It's db1.dbo.TempTable and db2.dbo.TempTable

The four-part naming scheme goes:

ServerName.DatabaseName.Schema.Object

Aaron Alton
Awesome that is good to know! Much appreciated!
gmcalab
A: 

Try this

INSERT INTO dbo.DB1.TempTable
    (COLUMNS)
    SELECT COLUMNS_IN_SAME_ORDER FROM dbo.DB2.TempTable

This will only fail if an item in dbo.DB2.TempTable is in already in dbo.DB1.TempTable.

David Basarab
+3  A: 

SELECT ... INTO creates a new table. You'll need to use INSERT. Also, you have the database and owner names reversed.

INSERT INTO DB1.dbo.TempTable
SELECT * FROM DB2.dbo.TempTable
Tom H.
Awesome thanks so much this is what I was looking for!
gmcalab
I thought I had it but I got this error. Any work around for that ?An explicit value for the identity column in table 'DB1.dbo.TempTable' can only be specified when a column list is used and IDENTITY_INSERT is ON.
gmcalab
I tried this:SET IDENTITY_INSERT DB1.dbo.TempTable ONGOINSERT INTO DB1.dbo.TempTableSELECT * FROM DB2.dbo.TempTable-- Disable IDENTITY_INSERT.SET IDENTITY_INSERT DB1.dbo.TempTable OFFGO
gmcalab
I got it, the last post by Goblyn27 was very helpful too!!!! +1
gmcalab
I'm glad that you got it figured out
Tom H.
+1  A: 

SELECT * INTO requires that the destination table not exist.

Try this.

INSERT INTO db1.dbo.TempTable
 (List of columns here)
SELECT (Same list of columns here)
FROM db2.dbo.TempTable
Mitchel Sellers
+1  A: 

Hard to say without any idea what you mean by "it didn't work." There are a whole lot of things that can go wrong and any advice we give in troubleshooting one of those paths may lead you further and further from finding a solution, which may be really simple.

Here's a something I would look for though,

Identity Insert must be on on the table you are importing into if that table contains an identity field and you are manually supplying it. Identity Insert can also only be enabled for 1 table at a time in a database, so you must remember to enable it for the table, then disable it immediately after you are done importing.

Also, try listing out all your fields

INSERT INTO db1.user.MyTable (Col1, Col2, Col3)
SELECT Col1, COl2, Col3 FROM db2.user.MyTable
Goblyn27
wow, three people beat me to idea #2 while I was typing. GOd I am sloooooowwww
Goblyn27