﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / How can I set identity_insert on a tablename passed to an sproc / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 01:42:50 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: How can I set identity_insert on a tablename passed to an sproc</title><link>http://www.sqlservercentral.com/Forums/Topic1038529-392-1.aspx</link><description>Thank you - all great answers. I really appreciate the time you put into helping me.</description><pubDate>Mon, 27 Dec 2010 10:26:51 GMT</pubDate><dc:creator>Don Bernstein</dc:creator></item><item><title>RE: How can I set identity_insert on a tablename passed to an sproc</title><link>http://www.sqlservercentral.com/Forums/Topic1038529-392-1.aspx</link><description>[quote][b]Don Bernstein (12/22/2010)[/b][hr]SET IDENTITY_INSERT IDDemo OFFEXEC ('SET IDENTITY_INSERT IDDemo ON')INSERT IDDemo (pk_ID, Textdata) Values (4, 'This fails')When I run it (sql2005), I get an error: Cannot insert explicit value for identity column... Only one row is returned.[/quote]IDENTITY_INSERT is a session level option, and essentially anything you run with with EXEC runs in a new session. So like the other people said, and provided examples for, you would need to put anything that relies on the IDENTITY_INSERT option into the dynamic SQL, but make sure you watch out for SQL injections, since to set the IDENTITY_INSERT option requires a fair amount of privileges. Of course to be of any value your INSERT would have to by dynamic as well, since I assume that you want to insert into the table passed as a parameter.Another option, if the set of tables is know, is to use IF statements to turn the option on for the correct table and INSERT the record. Yes, it is more code to manage but you wouldn't have to worry about SQL injection.</description><pubDate>Wed, 22 Dec 2010 22:17:26 GMT</pubDate><dc:creator>UMG Developer</dc:creator></item><item><title>RE: How can I set identity_insert on a tablename passed to an sproc</title><link>http://www.sqlservercentral.com/Forums/Topic1038529-392-1.aspx</link><description>since you said you are building a string, this is probably what you want for a demo:[code]CREATE TABLE IDDemo( pk_ID Integer IDENTITY(1,1), Textdata Varchar(100) )DECLARE @sql varchar(max)declare @tablename varchar(30)SET @sql = 'SET IDENTITY_INSERT @TheDynamicTableName ON;INSERT @TheDynamicTableName (pk_ID, Textdata) Values (4, ''This works too'');SET IDENTITY_INSERT @TheDynamicTableName OFF;'SET @tablename = 'IDDemo'SET @sql = REPLACE(@sql,'@TheDynamicTableName',@tablename)PRINT @sqlEXEC (@sql)SELECT * FROM IDDemo[/code]</description><pubDate>Wed, 22 Dec 2010 17:01:27 GMT</pubDate><dc:creator>Lowell</dc:creator></item><item><title>RE: How can I set identity_insert on a tablename passed to an sproc</title><link>http://www.sqlservercentral.com/Forums/Topic1038529-392-1.aspx</link><description>Don i think your EXC command needs to do everything all together; you can separate commands with a semi colon, which is rumored to be mandatory in a future version of SQL.this works for example:[code]CREATE TABLE IDDemo( pk_ID Integer IDENTITY(1,1), Textdata Varchar(100) )EXEC ('SET IDENTITY_INSERT IDDemo ON;INSERT IDDemo (pk_ID, Textdata) Values (4, ''This works too'');SET IDENTITY_INSERT IDDemo OFF;')SELECT * FROM IDDemo[/code]</description><pubDate>Wed, 22 Dec 2010 16:33:08 GMT</pubDate><dc:creator>Lowell</dc:creator></item><item><title>How can I set identity_insert on a tablename passed to an sproc</title><link>http://www.sqlservercentral.com/Forums/Topic1038529-392-1.aspx</link><description>I have the tablename in a @variable. I build the SET IDENTITY_INSERT and execute it. There's no error, but a subsequent insert fails. The code below illustrates the problem.CREATE TABLE IDDemo	(	pk_ID	Integer IDENTITY(1,1)	,	Textdata Varchar(100)	)SET IDENTITY_INSERT IDDemo OFFSET IDENTITY_INSERT IDDemo ONINSERT IDDemo (pk_ID, Textdata) Values (3, 'This works')SET IDENTITY_INSERT IDDemo OFFEXEC ('SET IDENTITY_INSERT IDDemo ON')INSERT IDDemo (pk_ID, Textdata) Values (4, 'This fails')Select *From IDDemoDrop Table IDDemoWhen I run it (sql2005), I get an error: Cannot insert explicit value for identity column... Only one row is returned.</description><pubDate>Wed, 22 Dec 2010 15:23:01 GMT</pubDate><dc:creator>Don Bernstein</dc:creator></item></channel></rss>