?
Solved

sql  stored procedure + Return the id of the last inserted record

Posted on 2011-02-15
2
Medium Priority
?
920 Views
Last Modified: 2012-05-11
I have this code In my stored procedure.  I want to get the value of the id for the record that was just inserted so that i can use it to insert into another table. How would i do so?

 IF @ImageName      <> ''
      begin
  INSERT INTO Images(Image_Data, Image_Name, Content_Type)
      VALUES (@ImageData, @ImageName, @ImageContentType)
      
end
 

0
Comment
Question by:vbnetcoder
2 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 34902498

try this

declare @LastID int

IF @ImageName      <> ''
begin
  INSERT INTO Images(Image_Data, Image_Name, Content_Type)
  VALUES (@ImageData, @ImageName, @ImageContentType)

  set @LastID = @@Identity
END
0
 

Author Closing Comment

by:vbnetcoder
ID: 34902568
ty
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

839 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