What the question title says. With a query such as SELECT @@IDENTITY AS ins_id
, do I need to supply the table name or any other info to specify which table/database i'm talking about?
views:
649answers:
2
+1
A:
No; it works much like SELECT LAST_INSERT_ID() in mysql, retrieving the last identity value inserted. You may want to take a look at this in-depth examination for more on what you might want to be concerned about with it.
chaos
2009-02-22 12:44:38
+3
A:
@@IDENTITY
returns the most recent identity generated in the current session. In most cases you'll probably want to use SCOPE_IDENTITY
instead, which returns the most recent identity generated in the current scope.
For example, if you insert a row into table1, but that insert fires a trigger which inserts a row into table2, then @@IDENTITY
will return the identity from table2 whereas SCOPE_IDENTITY
will return the identity from table1.
INSERT INTO my_table (my_column) VALUES ('test')
-- return the most recent identity from my_table
-- regardless of any other inserts done by triggers etc
SELECT SCOPE_IDENTITY() AS ins_id
LukeH
2009-02-22 12:54:31