user2599269 user2599269 - 3 months ago 14x
SQL Question

Sql Server return the value of identity column after insert statement

Is it possible in sql server using stored procedure to return the identity column value in a table against which some values are inserted? For example using stored procedure if we insert data in a table:

Table TBL

  • UserID integer, identity, auto-incremented

  • Name varchar

  • UserName varchar

  • Password varchar

So if I run the store procedure inserting some values like:

Insert into TBL (Name, UserName, Password)
Values ('example', 'example', 'example')

how can I return the value of UserID at which this insertion will take place. I need The value of UserID for some other operations, can anybody solve this?

Insert into TBL (Name, UserName, Password) Output Inserted.IdentityColumnName
 Values ('example', 'example', 'example')