Best way to get identity of inserted row?

后端 未结 14 2421
醉梦人生
醉梦人生 2020-11-21 07:06

What is the best way to get IDENTITY of inserted row?

I know about @@IDENTITY and IDENT_CURRENT and SCOPE_IDENTITY

14条回答
  •  不思量自难忘°
    2020-11-21 07:54

    MSDN

    @@IDENTITY, SCOPE_IDENTITY, and IDENT_CURRENT are similar functions in that they return the last value inserted into the IDENTITY column of a table.

    @@IDENTITY and SCOPE_IDENTITY will return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.

    IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope. For more information, see IDENT_CURRENT.

    • IDENT_CURRENT is a function which takes a table as a argument.
    • @@IDENTITY may return confusing result when you have an trigger on the table
    • SCOPE_IDENTITY is your hero most of the time.

提交回复
热议问题