Solved

IDENTITY_INSERT in Stored Procedure

Posted on 2006-11-04
4
869 Views
Last Modified: 2008-02-01
Hi,
I want to insert a record into a table which is having an identity column.
when i am explicitly setting the IDENTITY_INSERT ON, i am able to insert, but when i use the same in a stored procedure, it is not working. The below line is not working in the SP

set @sql='SET IDENTITY_INSERT '+@TableName+' ON'
EXEC (@sql)
here table name i am passing as parameter.
Can anybody please help me.

Thanks
Dhanraj
0
Comment
Question by:Dhanraj_k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17872969
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_set-set_7zas.asp

the issue is that if you put only the SET IDENTITY_INSERT  ... statement into th EXEC(), the effect of the SET will be lost when the EXEC is completed...

now, why do you want to use the table name as parameter? can you explain a bit more about your implementation requirements?
0
 
LVL 11

Expert Comment

by:rw3admin
ID: 17873761
you have to do your complete insert statement along with IDENTITY_INSERT on and off in one dynamic query

0
 

Author Comment

by:Dhanraj_k
ID: 17878830
hi angelIII,

my stored procedure is used to insert deleted records to different tables. It will accept the insert query, table name and a boolean IsIdentity as parameters.

if IsIdentity is true, then it will execute the following.
 set @IdentitySql='SET IDENTITY_INSERT '+@TableName+' ON'  
         EXEC (@IdentitySql)
         EXEC (@sInsertQuery)

Regards
Dhanraj
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
ID: 17879133
as said, you will need to run all in one:

set @IdentitySql=' SET IDENTITY_INSERT '+@TableName+' ON '  
set @IdentitySql2=' SET IDENTITY_INSERT '+@TableName+' OFF '  

EXEC (@IdentitySql  +  @sInsertQuery + @IdentitySql2  )

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question