Scope_identity() in batched sqlclient commands

孤街浪徒 提交于 2019-12-02 06:25:22

From the documentation for SCOPE_IDENTITY (emphasis mine):

Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.

In this case, your command:

"insert into .... ; select SCOPE_IDENTITY();"

Is a batch, and therefore, you will get the last inserted identity value in that batch, which in this case is the identity value in the insert, since it's the only other statement in the batch.

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