@@IDENTITY after INSERT statement always returns 0

后端 未结 13 1313
一整个雨季
一整个雨季 2021-01-11 13:00

I need a function which executes an INSERT statement on a database and returns the Auto_Increment primary key. I have the following C# code but, while the INSERT statement w

13条回答
  •  日久生厌
    2021-01-11 13:14

    you need to return the identity at the same time as you open the initial connection. Return a result set from your insert or an output variable.

    You should also always use SCOPE_IDENTITY() not @@identity. Reference here

    You should add

    SELECT SCOPE_IDENTITY() 
    

    After the insert.

提交回复
热议问题