Solved

IDENTITY_INSERT in Stored Procedure

Posted on 2006-11-04
4
855 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
  • 2
4 Comments
 
LVL 142

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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now