Solved

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

Posted on 2011-02-15
2
912 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
[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
2 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya 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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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