Return the id of the just added row

前端 未结 5 1333
广开言路
广开言路 2020-12-21 13:03

In a similar vein to my previous question I again ask the SO guys for your collective wisdom and help.

In a stored procedure and after passing some checks I need to i

相关标签:
5条回答
  • 2020-12-21 13:15

    SELECT SCOPE_IDENTITY()

    0 讨论(0)
  • 2020-12-21 13:17
    SELECT @CaseID = SCOPE_IDENTITY()
    

    In fact, you can just do (if that's the end of the stored proc.):

    SELECT SCOPE_IDENTITY()
    

    (The OUTPUT clause is only available in SQL Server 2005 onwards...)

    Ref: SCOPE_IDENTITY

    0 讨论(0)
  • 2020-12-21 13:18

    scope_identity()

    0 讨论(0)
  • 2020-12-21 13:31

    You need to use the OUTPUT clause

    http://blog.jemm.net/articles/databases/how-to-using-sql-server-2005s-output-to-return-generated-identity/

    ...which, as pointed out, is only available in sqlserver 2005. Plz disregard.

    0 讨论(0)
  • 2020-12-21 13:36

    As others mentioned, SCOPE_IDENTITY() is the way to go, though some ORM tools provide this functionality as well.

    The only thing you need to remember is SCOPE_IDENTITY() will return the last identity key value generated during the current session only. This is useful in filtering out new keys which may have been created by other clients simultaneously. SELECT @@IDENTITY will return the last key generated by any client/session.

    0 讨论(0)
提交回复
热议问题