@@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 etcSELECT SCOPE_IDENTITY() AS ins_id


