56

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?

Rich Seller
  • 83,208
  • 23
  • 172
  • 177
Gabe
  • 49,577
  • 28
  • 142
  • 181

13 Answers13

73

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
  • 46,766
  • 14
  • 87
  • 128
  • 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. – Gabe Jun 02 '09 at 18:15
  • I tried this: SET IDENTITY_INSERT DB1.dbo.TempTable ON GO INSERT INTO DB1.dbo.TempTable SELECT * FROM DB2.dbo.TempTable -- Disable IDENTITY_INSERT. SET IDENTITY_INSERT DB1.dbo.TempTable OFF GO – Gabe Jun 02 '09 at 18:17
  • Let's say TempTable are equal in both databases, but TempTable from DB2 has an extra column (with constraint not null). How would you copy data from db1.TempTable to db2.TempTable, adding a default value to it's extracolumn (i.e., "True") ? – Twimnox Sep 27 '18 at 14:08
  • You should create a new question for this (after searching to make sure that it's not answered elsewhere). In this way other people with the same problem can more easily find an answer in the future. They wouldn't be able to find it as easily if it's buried in comments in response to another answer. – Tom H Sep 28 '18 at 15:44
  • I found this real useful as a complement when Ids need to match: https://stackoverflow.com/questions/2005437/an-explicit-value-for-the-identity-column-in-table-can-only-be-specified-when-a – RKrogh Jan 16 '19 at 12:43
  • What if I only need 10 rows? – Shravya Mutyapu Sep 20 '21 at 14:26
  • Do you have some reason to suspect that this wouldn't work with only 10 rows? Have you tried using it? – Tom H Sep 22 '21 at 14:39
20

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
  • 62,228
  • 14
  • 110
  • 173
9

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

The four-part naming scheme goes:

ServerName.DatabaseName.Schema.Object

Aaron Alton
  • 22,728
  • 6
  • 34
  • 32
5

We can three part naming like database_name..object_name

The below query will create the table into our database(with out constraints)

SELECT * 
INTO DestinationDB..MyDestinationTable 
FROM SourceDB..MySourceTable 

Alternatively you could:

INSERT INTO DestinationDB..MyDestinationTable 
SELECT * FROM SourceDB..MySourceTable

If your destination table exists and is empty.

Deepak Kothari
  • 1,601
  • 24
  • 31
5

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
Tom H
  • 46,766
  • 14
  • 87
  • 128
Peter Lange
  • 2,786
  • 2
  • 26
  • 40
3

Don't forget to insert SET IDENTITY_INSERT MobileApplication1 ON to the top, else you will get an error. This is for SQL Server

SET IDENTITY_INSERT MOB.MobileApplication1 ON
INSERT INTO [SERVER1].DB.MOB.MobileApplication1 m
      (m.MobileApplicationDetailId,
       m.MobilePlatformId)
SELECT ma.MobileApplicationId,
       ma.MobilePlatformId 
FROM [SERVER2].DB.MOB.MobileApplication2 ma
Sabri Meviş
  • 2,231
  • 1
  • 32
  • 38
2

Im prefer this one.

INSERT INTO 'DB_NAME' 
(SELECT * from 'DB_NAME@DB_LINK')
MINUS 
(SELECT * FROM 'DB_NAME');

Which means will insert whatsoever that not included on DB_NAME but included at DB_NAME@DB_LINK. Hope this help.

Paul
  • 4,160
  • 3
  • 30
  • 56
Leon
  • 21
  • 1
1
INSERT INTO DB1.dbo.TempTable
SELECT * FROM DB2.dbo.TempTable

If we use this query it will return Primary key error.... So better to choose which columns need to be moved, like

INSERT INTO db1.dbo.TempTable // (List of columns here)
SELECT (Same list of columns here)
FROM db2.dbo.TempTable
yennsarah
  • 5,467
  • 2
  • 27
  • 48
0

This works successfully.

INSERT INTO DestinationDB.dbo.DestinationTable (col1,col1)
 SELECT Src-col1,Src-col2 FROM SourceDB.dbo.SourceTable
Ghebrehiywet
  • 884
  • 3
  • 12
  • 20
0

You can copy one table to other db table even with some additional columns.

insert into [SchoolDb1].[dbo].Student(Col1, Col2,Col3, CreationTime, IsDeleted) 
select Col1, Col2,Col3,,getdate(),0 from [SchoolDb2].[dbo].Student

These are additional columns: (CreationTime is datatime and IsDeleted is boolean)

Shahid Malik
  • 163
  • 13
0
select * from DBA1.TABLENAMEA;

create table TABLENAMEA as (select * from DBA1.TABLENAMEA);
Julian
  • 6,586
  • 2
  • 9
  • 33
0

These manual way provides more flexibility, but at the same time, works for table whose size is smaller to few thousands.

Do select * from <table name> from DB, once whole table is displayed, scroll till it's bottom.

Right click and do Export table as Insert statement, provide the name of the destination table and export the table as .sql file.

Use any text editor to further do regular find and replace operation to include more column names etc.

Use the INSERT statement in destination DB.

Om Sao
  • 7,064
  • 2
  • 47
  • 61
0

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
  • 72,212
  • 42
  • 129
  • 156