Solved

IDENTITY_INSERT in Stored Procedure

Posted on 2006-11-04
4
854 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

758 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

20 Experts available now in Live!

Get 1:1 Help Now