12/11/13
Inserting records into a table with IDENTITY column using INSERT INTO SELECT statementConsidering the following tables (table_1 with IDENTITY column): IF (object_id('table_1') IS NOT Null) drop table table_1 CREATE TABLE table_1 ( Id bigint IDENTITY(1,1) NOT NULL, Col1 nvarchar(4000) NOT NULL, Col2 nvarchar(4000) NOT NULL ) Id (identity) Col1 Col2 ----------- ---- ----
IF (object_id('table_2') IS NOT Null) drop table table_2 CREATE TABLE table_2 ( Col1 nvarchar(4000) NOT NULL, Col2 nvarchar(4000) NOT NULL ) Col1 Col2 ---- ----
Insert some data into table_2: INSERT INTO table_2 VALUES ('some text', 'some other text') INSERT INTO table_2 VALUES ('some text', 'some other text') INSERT INTO table_2 VALUES ('some text', 'some other text')
...
The following syntax must be used when trying to populate table_1 with records from table_2 using INSERT INTO SELECT statement: INSERT INTO table_1 (Col1, Col2) SELECT Col1, Col2 FROM table_2
The result will look something like this: select * from table_1
No feedback yet |
|