Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

sql server - What is the scope of SET IDENTITY_INSERT xyz ON?

What is the scope of SET IDENTITY_INSERT xyz ON?

If I use this in one stored procedure for a certain table, what happens if a different user running a different procedure, inserts into that certain table at the same time?

Also, what happens if different users/procedures try to set SET IDENTITY_INSERT xyz ON for different tables at the same time?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's a session option a table can only have the option on for any one table at a time but multiple different sessions can have it on for the same table (not sure that would ever be a good idea though!)

When a child batch finishes (that sets this option) it looks like it automatically gets unset for the connection.

CREATE TABLE Tst
(C INT IDENTITY(1,1))

EXEC('SET IDENTITY_INSERT Tst ON')
INSERT INTO Tst(C) VALUES (1) /*Fails - Complains IDENTITY_INSERT is off*/

SET IDENTITY_INSERT Tst ON
EXEC('INSERT INTO Tst(C) VALUES (1)') /*Succeeds this way round*/
SET IDENTITY_INSERT Tst OFF


SET IDENTITY_INSERT Tst ON
EXEC('SET IDENTITY_INSERT Tst ON; INSERT INTO Tst(C) VALUES (1);') /* Also succeeds like this*/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...