?
Solved

Need to perform a DB Insert then return an ID?

Posted on 2014-01-17
11
Medium Priority
?
266 Views
Last Modified: 2014-01-20
I need to write a Stored Procedure that will Insert a record into a Table and then return the Primary Key and I'm not sure how to do that. I'm using SQL Server 2008.
0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
11 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1000 total points
ID: 39789462
CREATE PROC sp_foo AS

INSERT INTO Customer(fk_customer_type_id, name)
VALUES (1, 'Jack Wagon')

-- Return the identity field generated by the above insert
SELECT SCOPE_IDENTITY()

GO

Open in new window

0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 1000 total points
ID: 39789463
if your table has an identity column and is the PK ,  perform the insert first and use SCOPE_IDENTITY() to get the id

INSERT into ...
SELECT SCOPE_IDENTITY()
0
 

Author Comment

by:BlakeMcKenna
ID: 39789766
Thanks guys...that worked!
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 66

Expert Comment

by:Jim Horn
ID: 39789792
Okay, I'll bite.  How did my comment not correctly answer the question?
0
 

Author Comment

by:BlakeMcKenna
ID: 39789837
Jim,

Your answer was correct. Since there were 2 correct answers, I accepted multiple answers.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39789849
I'm showing that only the second answer was accepted, so if your intent was to split you'll need to re-open the question and re-award points.
0
 

Author Comment

by:BlakeMcKenna
ID: 39791068
How do I reopen a ?
0
 

Author Comment

by:BlakeMcKenna
ID: 39794256
Sorry...I'll redo it!
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39794266
Thanks for the split.  Good luck with your project.  -Jim
0
 

Author Comment

by:BlakeMcKenna
ID: 39794267
Your welcome...and thanks!
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question