我有一个存储过程,它有一个 merge .. into 语句。我正在尝试从存储的过程中获取主键的值。
SET @NewCompanyID = @CompanyID
....
merge company as c
using ( select @CompanyID) as compAlt (company_id )
on compAlt.company_id = c.company_id
when matched
then
update set ....
when not matched
then
insert ... ;
SET @NewCompanyID = @@identity;
如果合并语句运行更新,我想将@NewCompanyID 设置为传递到存储过程(@CompanyID)的任何值。如果执行了插入语句,那么我想传递@@identity。我该怎么做呢?