Solved

IDENTITY_INSERT in Stored Procedure

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query help 15 55
How can I find this data? 3 28
SQL- GROUP BY 4 25
Make an array show the subkey and put it in a query 2 29
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

733 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