I have two identical tables and need to copy rows from table to another. What is the best way to do that? (I need to programmatically copy just a few rows, I don't need to use the bulk copy utility).
6 Answers
As long as there are no identity columns you can just
INSERT INTO TableNew
SELECT * FROM TableOld
WHERE [Conditions]
-
27Be careful with this syntax as it won't work if Table2 has an identity column and it will break in the future if Table1 ever changes without Table2 changing in sync (burned me before). This solution might be perfect for your case, just be aware of these considerations. Commented Jul 31, 2009 at 12:00
-
12You can use
SET IDENTITY_INSERT < table > ON
(andSET IDENTITY_INSERT < table > OFF
) in order to temporarily disable the identity column on the table you're trying to insert into. Worked for me trying to restore a few missing records in the middle of dataset.– nickbCommented Dec 14, 2011 at 11:42 -
1what if the primary key in table1 exists in table2 as a foreign key? I have the same thing, and i don't know what to do.. in table2 id the foreign-key AS "Technology_idTechnology" and in table 1, it is there as "idTechnology" I want to copy all the entries from table1 to table2, when the idTechnology matches in both tables..– ZelelBCommented Mar 24, 2015 at 15:10
Alternative syntax:
INSERT tbl (Col1, Col2, ..., ColN)
SELECT Col1, Col2, ..., ColN
FROM Tbl2
WHERE ...
The select query can (of course) include expressions, case statements, constants/literals, etc.
-
2This is great when the tables differ slightly. I've a few extra columns in my second table and the accepted answer doesn't work with MSSQL as they need to be identical.– Tony MCommented May 8, 2015 at 14:24
Jarrett's answer creates a new table.
Scott's answer inserts into an existing table with the same structure.
You can also insert into a table with different structure:
INSERT Table2
(columnX, columnY)
SELECT column1, column2 FROM Table1
WHERE [Conditions]
-
6@ScottStonehouse: if you collect all the other answers into this answer like you have done but with code (making it comprehensive), you'd definitely become the best answer. Commented Jul 31, 2009 at 12:01
SELECT * INTO < new_table > FROM < existing_table > WHERE < clause >
-
2Is there any way to do this If this two tables are in diff databases in sql-server.– NareshCommented Jun 3, 2011 at 17:01
-
10Sure! Just fully qualify the database. [servername].[schema].[table]. For example
SELECT * INTO [SQLTEST].[dbo].[EMPLOYEES] FROM [SQLPROD].[dbo].[EMPLOYEES]
Commented Jun 6, 2011 at 11:07 -
5This creates a new table, which is not what the OP is asking for.– BrieCommented Feb 4, 2015 at 15:18
INSERT INTO DestTable
SELECT * FROM SourceTable
WHERE ...
works in SQL Server
-
3This works only when DestTable does not exist. You will get an error if DestTable is created before this query. Commented Aug 14, 2015 at 5:46
-
2Actually it fails if the destination table doesn't exist. It may cause errors if the existing DestTable isn't empty.– KaniuCommented Apr 13, 2016 at 12:18
To select only few rows..This will work like charm..
SELECT TOP 10 *
INTO db2.dbo.new_table
FROM db1.dbo.old_table;
Note : Just create a new table in the required db..We need not define its structure.