user2599269 user2599269 - 1 year ago 111
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?

Answer Source
Insert into TBL (Name, UserName, Password) Output Inserted.IdentityColumnName
 Values ('example', 'example', 'example')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download