Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

environment sql server 2005

In my stored proc i am calling another stored proc to insert several rows into several tables. The last row inserted i want to pass the identity of that row to the original stored proc. I am doing this by using return scope_identity(); My understanding is return is only used for error messages/status codes. Is this a "good" way of doing what I need to do, or is there another "better" way?

share|improve this question
up vote 3 down vote accepted

All procedures have an integer return value. Your method would work, there is no hard and fast rule against it.

Alternatively, you could return an output parameter.


create procedure YourProcedure
  @ID output
  insert into YourTable (bunch of values)
  select @ID = scope_identity()

You then call your procedure as follows:

declare @TheID int
exec YourProcedure @TheID output
select @TheID
share|improve this answer

There are 3 ways:

  • OUTPUT parameter (as per datagod's answer)
  • RETURN (as you noted)
  • A simple SELECT which can be consumed say with ExecuteScalar

I'd usually use an OUTPUT parameter because it's the lightest way.

Note: If you are inserting several rows into one table and only getting the last IDENTITY there is no guarantee that the previous rows are contiguous

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.