How do I insert into a table and get back the primary key value?
sql, sql-server, t-sql
Solution
insert into YourTable values (...)
get the new PK with scope_identity()
select scope_identity()
Problem
I have a primary key set up to auto increment. I am doing multiple queries and I need to retrieve that primary key value to use as a foreign key in another table (`IsIdentity = TRUE`). Is there any elegant way to get back the primary key value when I do an insert query? Right now I am requerying and getting the highest value in that column which seems really hacky. Any suggestions?