torsdag 5 november 2009

@@Identity with ASP

T-SQL note
@@Identity should be safe to use, unless you are using triggers, if you want to retrieve the latest ídentity value in a table.

Insert into myTable (Col1) Values('hello')
Select @@Identity

I know the @@identity is global within the connection, but it should be safe to use unless you are using triggers. When running from a ASP web application, there seem to be some connection pooling that makes this fail, or rather return the wrong ID. So if two concurrent users are calling the same procedure through an ASP application they might get each others id's.

Solution is as simple as the example: just use scope_identity() function instead or the output variable available in SQL2005 and later.

Inga kommentarer:

Skicka en kommentar