all groups > sql server programming > december 2004 >
You're in the

sql server programming

group:

Updating Identity columns in mulitple tables


RE: Updating Identity columns in mulitple tables David Portas
12/10/2004 6:35:06 AM
sql server programming:
Books Online says "At any time, only one table in a session can have the
IDENTITY_INSERT property set to ON". In other words you CAN enable
IDENTITY_INSERT for more than one table but you can only enable it for one
table at a time. Since you can only insert into one table per statement this
shouldn't be a problem. You just need to set IDENTITY_INSERT OFF for the
first table before you can set it ON for another.

Note that you can never UPDATE an IDENTITY column.

--
David Portas
SQL Server MVP
--
Re: Updating Identity columns in mulitple tables David Portas
12/10/2004 8:49:06 AM
You got it.

--
David Portas
SQL Server MVP
--
Re: Updating Identity columns in mulitple tables David Portas
12/10/2004 9:23:09 AM
The following works for me (SQL2000 Enterprise SP3a):

CREATE TABLE T1 (x INTEGER IDENTITY)
CREATE TABLE T2 (x INTEGER IDENTITY)

GO

CREATE PROC usp_test_insert_id
AS
SET IDENTITY_INSERT T1 ON
INSERT INTO T1 (x) VALUES (123)
SET IDENTITY_INSERT T1 OFF
SET IDENTITY_INSERT T2 ON
INSERT INTO T2 (x) VALUES (456)
SET IDENTITY_INSERT T2 OFF
SELECT x FROM T1
SELECT x FROM T2

GO

EXEC usp_test_insert_id

If you still have a problem then post some code (simplified as much as
possible) to reproduce the problem. Include the CREATE TABLE statement(s) for
your table(s).

--
David Portas
SQL Server MVP
Updating Identity columns in mulitple tables Arun
12/10/2004 7:52:30 PM
Hi,

Is there a way we can insert rows to more than one table having identity
columns, in a single session? The documentation on SET IDENTITY_INSERT says
we can use this on only one table per session. I need to update multiple
tables with identity columns in a single session.

Thanks for any help
Arun

Re: Updating Identity columns in mulitple tables Arun
12/10/2004 9:29:42 PM
Do you mean I can Set it on for a table , insert the rows in that , set the
Identity_insert off on that table and continue doing the same with the next
table? that's great. I'll try that.

Arun

[quoted text, click to view]

Re: Updating Identity columns in mulitple tables Arun
12/10/2004 10:23:43 PM
That is exactly my problem. Inside a single stored procedure, I need to
insert rows in two or more tables with identity column. setting it 'OFF'
doesn't work.

Arun


[quoted text, click to view]

Re: Updating Identity columns in mulitple tables Arun
12/10/2004 10:28:17 PM
It didn't work David. I need to insert rows to more than one table with
Identity column inside a single stored proc. I tried setting it ON and OFF ,
but it works only for the first table.


[quoted text, click to view]

Re: Updating Identity columns in mulitple tables Arun
12/12/2004 12:04:37 PM
Thanks David. The problem with my query is that, I don't specify a column
list in the insert statement

I use,
Insert table1 select * from table2
where the schema of both table2 and table1 are similar. The error message
says, "An explicit value for the identity column in table1 can only be
specified when a column list is used and IDENTITY_INSERT is ON."
I cannot specify the columns explicity becoz I'm using dynamic queries and
the 'table1' and the 'table2' come through the variables. And, the 'table1'
is a temporary table created dynamically using the "Select into table1 from
table2" syntax.

Is there any other solution you can think of?

Regards,
Arun


[quoted text, click to view]

AddThis Social Bookmark Button