I have a stored proc with two insert statements. I would like to return the ID from the first insert. It looks broadly like this:
declare @retVal int
Begin transaction
insert into myTable(..) values (..)
set @retVal = scope_identity()
insert into myTable2(..) values(..)
Commit transaction
return @retVal
When I do this, the insert statements work fine but the same value is returned every time (in this case, ID 6 from myTable, for some strange reason.)
Would anyone have a guess on what's going on?
SELECTis better? You want to require all the scaffolding of retrieving a resultset, potentially using a DataReader, etc. to retrieve a single, scalar value?OUTPUTis far more appropriate in this case.IDENTITYcolumn or is the ID created/populated via a trigger?selectinstead of return is unlikely to fix it. Need to see the full picture.myTable? Could you share it? Could you also share the code where you are retrieving the return value and seeing 6 every time?