Return the id of the just added row

自古美人都是妖i 提交于 2019-11-29 15:53:43
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

scope_identity()

SELECT SCOPE_IDENTITY()

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!