how to return the value of identity column after insert statement in Sql Server

Priya
Priya
Participant
936 Points
28 Posts

I am using stored procedure to return the identity column value in a table against the row in which some vlaues are inserted. for example using stored procedure if we insert data in a table:

Table tblUser

UserID integer, identity, auto-incremented
Name varchar
UserName varchar
Password varchar

So if I run the store procedure inserting some values in the table tblUser like,

INSERT INTO tblUser(Name, UserName, Password) VALUES ('example', 'example', 'example')

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

 

Views: 8759
Total Answered: 1
Total Marked As Answer: 0
Posted On: 18-May-2016 02:46

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         

Use Output clause in insert statement as:

Insert into tblUser(Name, UserName, Password) Output Inserted.UserID
Values ('example', 'example', 'example')

 

Posted On: 18-May-2016 03:03
 Log In to Chat