October 2, 2018 at 12:51 pm
I'm trying to insert records from one table in to another table like the example below. The two tables have identical schema, except table1 has an extra column that is a primary key and an identity seed that increments by 1. Can I just use an insert statement like the one below to insert the records from table2 or do I need to add something to create the primary key field in table2?
Code:
select * into table1 from table2
October 2, 2018 at 12:56 pm
First of all, why????
secondly, you'd need to set identity insert on before inserting to the second table (I assume there is an identity column on it, but it's not clear). Then you should list out columns names i.e. Insert into TableB (Col1, Col2...) select Col1, Col2... from TableA
For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
October 2, 2018 at 1:07 pm
If you want to add an IDENTITY column in table1 that is not in table2, you can do this:
select IDENTITY(int, 1, 1) AS id, *
into table1
from table2
You don't have to make it a pk, but you could, using the standard way to create a PK:
ALTER TABLE dbo.table1 ADD CONSTRAINT ... PRIMARY KEY ( id ) ...
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
October 2, 2018 at 1:14 pm
I think the OP is saying that both tables already exist. If that is the case, you can't use the INTO clause, you need to use an INSERT statement. If the extra column is already an IDENTITY then SQL Server will handle the population of those values.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
October 2, 2018 at 1:40 pm
Thom A - Tuesday, October 2, 2018 1:14 PMI think the OP is saying that both tables already exist. If that is the case, you can't use the INTO clause, you need to use an INSERT statement. If the extra column is already an IDENTITY then SQL Server will handle the population of those values.
And you need to specify all of the columns EXCEPT the identity column in the destination table - or you set identity insert on the table and specify the identity value to be inserted in the SELECT.
INSERT INTO table1 (list of columns here - excluding identity column)
SELECT {list all columns here} FROM table2;
INSERT INTO table1 (list all columns here)
SELECT identity(int, 1, 1) AS identity_column, {rest of columns here} FROM table2;
Either way - if you are inserting data into a table that has an identity column, you must include the list of columns being inserted.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
October 2, 2018 at 7:39 pm
scotsditch - Tuesday, October 2, 2018 12:51 PMCan I ... ?
Did you try it before asking?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 3, 2018 at 4:38 am
Jeffrey Williams 3188 - Tuesday, October 2, 2018 1:40 PMEither way - if you are inserting data into a table that has an identity column, you must include the list of columns being inserted.
This isn't true, actually. Try the following.
I've tested, and this works on SQL Server 2008, 2012 and 2017. Provided that the 2 datasets have the same definition (excluding the IDENTITY) then SQL Server is "clever" enough to know how to handle the INSERT. I however, don't recommend it. You should always supply a list of columns for an INSERT. For example, if the destination table's definition changes, the INSERT can (probably) continue to work without being amended (provided the new column(s) are NULLable, and a referenced column doesn't have a breaking change, etc).
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply