get new SQL record ID

前端 未结 8 2033
南方客
南方客 2020-12-17 19:31

How can I get back the autogenerated ID for a new record I just inserted? (Using ASP classic and MSSQL 2005)

8条回答
  •  無奈伤痛
    2020-12-17 19:52

    SELECT @@Identity or SELECT SCOPE_IDENTITY() both work however Selecting SCOPE_Identity() is safer because it returns the last auto generated ID within your current scope. So for example assume we have a table called ScopeIDTable and on this table we have a trigger. This trigger will insert into a record into TriggerIdTable both tables have an auto increment column.

    If you use SELECT @@Identity you will get the last auto increment in that session which would be the Id generated from within the trigger (TriggerIdTable).

    If you use SELECT SCOPE_IDENTITY() you will get the id from your ScopeIdTable.

提交回复
热议问题