SQL – Insert but keep identity

I have two tables A and B with the same structure and I am trying to copy selected records from table A to table B including the identity column values.

(Here identitycolumn is ‘reqid’)

Solution:
SET IDENTITY_INSERT dbo.[B] ON
insert into dbo.[B]( col1, col2, col3, …, coln ) –List the names of columns
select * from dbo.[A] where reqid = 201
SET IDENTITY_INSERT dbo.[B] OFF

http://www.sqlservercentral.com/Forums/Topic1270417-149-1.aspx

Category: SQL