Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to SET IDENTITY_INSERT ON in SQL Server 2008 for multiple tables at once

I have two tables tblData1 and tblData2 and now I want to migrate records from another table with identity insert and I am trying to run a command as shown below

SET IDENTITY_INSERT LP1.dbo.tblData1 ON
GO

SET IDENTITY_INSERT LP1.dbo.tblData2 ON
GO

INSERT INTO LP1.DBO.tblData1 (ID,DATA)
SELECT ID,DATA FROM LP.DBO.tblData1
GO

INSERT INTO LP1.DBO.tblData2 (ID,DATA)
SELECT ID,DATA FROM LP.DBO.tblData2
GO

SET IDENTITY_INSERT LP1.dbo.tblData1 OFF
GO

SET IDENTITY_INSERT LP1.dbo.tblData2 OFF
GO

But it is showing error as below

IDENTITY_INSERT is already ON for table 'Sample_Training.dbo.tblData1'. Cannot perform SET operation for table 'dbo.tblData2'

Is it possible to perform multiple IDENTITY_INSERT at time in SQL Server 2008

like image 215
Dinesh Reddy Alla Avatar asked Dec 23 '14 06:12

Dinesh Reddy Alla


People also ask

How do you turn on identity insert?

Enabling the property “Enable Identity Insert” by checking the checkbox allows the values to be inserted in the identity field. This way, the exact identity values are moved from source database to the destination table.

What is when IDENTITY_INSERT is set to off?

IDENTITY_INSERT off in SQL Server Once you have turned the IDENTITY_INSERT option OFF, you cannot insert explicit values in the identity column of the table. Also, the value will be set automatically by increment in the identity column if you try to insert a new record.

How can insert data in identity column in SQL Server?

To manually insert a new value into the Id column, we first must set the IDENTITY_INSERT flag ON as follows: SET IDENTITY_INSERT Students ON; To set the IDENTIT_INSERT flag ON we need to use the SET statement followed by the flag name and the name of the table.


3 Answers

At any time, only one table in a session can have the IDENTITY_INSERT property set to ON.

So before enabling the other one, you should turn of existing if any.
If it is lesser number of tables you can turn on and turn off before and after your operations.
If the table count is huge, you should automate somehow to enable and disable before your operations.

like image 183
knkarthick24 Avatar answered Oct 18 '22 03:10

knkarthick24


Did you try changing the order

go

SET IDENTITY_INSERT LP1.dbo.tblData1 ON

INSERT INTO LP1.DBO.tblData1
            (ID,DATA)
SELECT ID,DATA
FROM   LP.DBO.tblData1

SET IDENTITY_INSERT LP1.dbo.tblData1 OFF

GO

SET IDENTITY_INSERT LP1.dbo.tblData2 ON

INSERT INTO LP1.DBO.tblData2
            (ID,DATA)
SELECT ID,DATA
FROM   LP.DBO.tblData2

SET IDENTITY_INSERT LP1.dbo.tblData2 OFF

GO 
like image 4
Pரதீப் Avatar answered Oct 18 '22 03:10

Pரதீப்


You can only set Identity_Insert for one table at a time in a single session. If there are no data dependancies between the tables, then you can open several sessions, each handling a different set of tables. Each session can set one table for identy_insert.

like image 1
Amir Pelled Avatar answered Oct 18 '22 05:10

Amir Pelled