Skip to content
Advertisement

Does having two or more calls to scope_identity() in a single procedure work?

Does having 2 or more calls to scope_identity() in a single procedure work?

For example:

declare @id_1 as int
declare @id_2 as int

insert into table_1 (...) 
values (...)

set @id_1 = select scope_identity()

insert into table_2 (...) 
values (...)

set @id_2 = select scope_identity()

For sure @id_1 gets the last inserted id of table_1. The question is, does @id_2 get the last inserted id of table_2?

Advertisement

Answer

Yes, sure – if both table_1 and table_2 for an identity column – why not??

But please use this syntax:

select @id_1 = scope_identity()

No need for both a set and a select

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement