Hi Greetings,
I'm Creating an dynamic SQL which inserts data from one DB to another DB having same Schema. For tables which are having Identity Column had to turn the IDENTITY_INSERT option ON. But its seems like its not working. The statement is executing successfully but Insert statement is giving error of Identity insert.
SELECT @Sql = 'SET IDENTITY_INSERT '+@DBName+'.[dbo].'+QUOTENAME(@tablename)+' OFF; '+
'SET IDENTITY_INSERT '+@DBName+'.[dbo].'+QUOTENAME(@tablename)+' ON; '+
'INSERT INTO '+@DBName+'.[dbo].'+QUOTENAME(@tablename)+' Select * from '+@SDBName+'.[dbo].'+QUOTENAME(@tablename)+'; '+
'SET IDENTITY_INSERT '+@DBName+'.[dbo].'+QUOTENAME(@tablename)+' OFF; '
EXEC (@Sql)
I'm using sql server 2005 Any Idea or help?
Your response would be highly appreciated.
Thanks.
Loading

Suthish NairPosted Jul 1, 2011, 1:26 PM
I forgot to mention, the key point here is the column names. You need to pass the column names during identity insert.
Suthish NairPosted Jul 1, 2011, 9:17 AM
Amit ChoudharyPosted Jul 1, 2011, 9:09 AM
Well My problem is little different i'm doing the same as Abhimanyu mentioned but in a different way.
I'm trying to execute the dynamic sql where we create sql as string and then Execute it using the
Exec() method in Sql Server.
and in this case the Set IDENTITY_INSERT [dynamic table name] ON; is not working not even giving any error.
Suthish NairPosted Jul 1, 2011, 8:19 AM
SQL Server Fun - 4 :: How to insert duplicate values to identity column