darogo
darogo

Reputation: 51

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?

Upvotes: 1

Views: 44

Answers (1)

marc_s
marc_s

Reputation: 754488

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...

Upvotes: 1

Related Questions