@@IDENTITY
返回当前会话中生成的最新标识。在大多数情况下,您可能需要使用SCOPE_IDENTITY
它,它返回当前作用域中生成的最新标识。
例如,如果您向 table1中 插入一行,但是该插入触发了将行插入到 table2中 的触发器,则@@IDENTITY
它将从 table2 返回标识,而SCOPE_IDENTITY
将从 table1 返回标识。
INSERT INTO my_table (my_column) VALUES ('test')
-- return the identity of the row you just inserted into my_table
-- regardless of any other inserts made by triggers etc
SELECT SCOPE_IDENTITY() AS ins_id