'SET IDENTITY_INSERT ' + @TablesName + ' OFF' doesnn't work


I have a big problem:
If I try to execute SET IDENTITY_INSERT as below,

DECLARE @SqlCheckIdentity  varchar (255)
SET @SqlCheckIdentity = 'SET IDENTITY_INSERT ' + @TablesName + ' ON'
EXEC (@SqlCheckIdentity)


DECLARE @SqlCheckIdentity  varchar (255)
SET @SqlCheckIdentity = 'SET IDENTITY_INSERT ' + @TablesName + ' OFF'
EXEC (@SqlCheckIdentity)

the command is not executed....in store procedure (in query analyzer say: Cannot insert explicit value for identity column in table '@TablesName'  when IDENTITY_INSERT is set to OFF).

I mean in this way I cannot set the identity ON/OFF

Do you know how to execute SET INSERT.... with parameters?

Thank a lot

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NievergeltSenior SW DevCommented:
Hi erncelen,

I believe, you ran full frontal into a "feature": after the EXEC the IDENTITY_INSERT status is reset.

You can circumvent this only by also putting the INSERT into the EXEC.
The following SP shows this:

DECLARE @t nvarchar(255), @s nvarchar(255)

SET @t = 'TestIdentityTable'

EXEC('CREATE TABLE ' + @t + ' (id int IDENTITY PRIMARY KEY, product varchar(40))')

-- Inserting values into table.
EXEC('INSERT INTO ' + @t + '(product) VALUES (''screwdriver'')')
EXEC('INSERT INTO ' + @t + '(product) VALUES (''hammer'')')
EXEC('INSERT INTO ' + @t + '(product) VALUES (''saw'')')
EXEC('INSERT INTO ' + @t + '(product) VALUES (''shovel'')')

-- Create a gap in the identity values.
EXEC('DELETE ' + @t + '  WHERE product = ''saw''')

-- Insert row in gap
EXEC('SET IDENTITY_INSERT ' + @t + ' ON INSERT INTO ' + @t + ' (id, product) VALUES(3, ''garden shovel'')SET IDENTITY_INSERT ' + @t + ' OFF')



Share and Enjoy    Christoph

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I tried the following sequence in SQL Query analizer and it worked:

DECLARE @SqlCheckIdentity  varchar (255)
DECLARE @TablesName  varchar (255)
set @TablesName = 'test'
SET @SqlCheckIdentity = 'SET IDENTITY_INSERT ' + @TablesName + ' ON'
EXEC (@SqlCheckIdentity)

You should set a value which is a table name for the @TablesName  variable (the line I added to your code). I used a table named "test" and it worked without problems.
erncelenAuthor Commented:
Thank to everybody for your suggestion,

now I can carry on with my job
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.