[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Get Key ID in Stored Procedure after Insert

If I have a database with a key field, and I want to insert a new row such as
 
Insert into Customer(Name, houseNum)
values(@name, @housenum)
 
then I want to get the key for that new row, what would be the best method for getting it?  Can I do it in the same stored procedure or do I have to make a new one?

Thank You!
-M
0
m_evergreen
Asked:
m_evergreen
1 Solution
 
TdlCommented:
Is the key an identity field?

Regards,
Marc

0
 
rafranciscoCommented:
If you have an identity field in your Customer table, you can use SCOPE_IDENTITY() to get the key assigned to the newly inserted record.  As an example:

DECLARE @ID INT

Insert into Customer(Name, houseNum)
values(@name, @housenum)

SET @ID = SCOPE_IDENTITY()
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now