Solved

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

Posted on 2011-02-15
2
909 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:
ewangoya earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

789 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