August 26, 2004 at 1:34 pm
August 26, 2004 at 1:58 pm
Take a look at scope_identity in books online. It may meet your needs.
"SCOPE_IDENTITY Returns the last IDENTITY value inserted into an IDENTITY column in the same scope. A scope is a module -- a stored procedure, trigger, function, or batch. Thus, two statements are in the same scope if they are in the same stored procedure, function, or batch"
Hope this helps
/Wayne
August 27, 2004 at 1:35 am
if your column is not IDENTITY qualified, you should consider encapsulation of the insertion in a stored proc with an output parameter returning the inserted value :
1° Begin transaction
2° inserting de record
3° getting max(id) into return parameter
4° commit transaction
August 27, 2004 at 2:13 am
Thanks for that.
The stored procedure will have to perform an EXEC sp_executesql, as I'm passing dynamic INSERT statements. The SP cannot be table specific.
Therefore, it would seem the INSERT is being performed in another scope and scope_identity() returns diddly squat.
I will have to wrap this in a transaction, and hope that solves the problem.
Thanks guys.
August 27, 2004 at 8:28 am
We use RETURN @@Identity as in the example below:
INSERT INTO tblUsers (FName, LName)
VALUES ('John', 'Smith')
RETURN @@Identity
GO
August 27, 2004 at 12:16 pm
Watch out for @@identity - if there is a trigger which causes rows to be inserted in another table you will get the id of the LAST row inserted, regardless of the table.
August 27, 2004 at 7:24 pm
OK, I hesitate to even offer this solution. Nevertheless, it's saved my bacon, and is useful although not optimal as far as performance is concerned:
---TEST Table
if object_ID('tempdb..##Test') is not null drop table ##Test
create table ##Test (ID int identity)
-----------------------------------------------
--declare temporary table to hold returned ID
if object_ID('tempdb..#ID') is not null drop table #ID
create table #ID (ID int)
--Create variable to hold ID
declare @ID int
--Populate temp table
Insert #ID
EXEC sp_executesql N'Insert ##Test Default values
select scope_identity()'
--Populate variable
select @ID = ID
From #ID
---Process Variable
select @ID
--clear temp table
truncate table #ID
Signature is NULL
August 30, 2004 at 11:22 am
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply