Friday, February 24, 2012

Inserting row and scope_identity()

With a query such as,

SET NoCount ON
INSERT INTO ...
VALUES ...
SELECT scope_identity()
SET NoCount OFF

, can I trust that the id I get back is for the row that I just inserted, or
can it be incorrect because of other "simultaneous" inserts? I need it for
an ASP page.

Thanks,
Marcus[posted and mailed, vnligen svara i nys]

Marcus (lumbus@.ludd.luth.se) writes:
> With a query such as,
> SET NoCount ON
> INSERT INTO ...
> VALUES ...
> SELECT scope_identity()
> SET NoCount OFF
> , can I trust that the id I get back is for the row that I just
> inserted, or can it be incorrect because of other "simultaneous"
> inserts? I need it for an ASP page.

800 grader, du kan lita p mej, du kan lita p mej.

Yes, you can trust it. scope_identity is local to your process - in
fact local to the scope (procedure, batch etc) you are in.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

No comments:

Post a Comment