您可以使用该OUTPUT
子句从多个插入的行中捕获身份。在下文中,我假设ServiceName
并且RequestName
足以唯一标识传入的值。如果不是,那么希望您可以调整以下内容(您没有在问题中真正定义任何可用的非身份列名称或值):
首先,设置表:
create table Requests (RId int IDENTITY(1,1) not null primary key,RequestName varchar(10) not null)
create table Services (SId int IDENTITY(1,1) not null primary key,ServiceName varchar(10) not null)
create table Mappings (MId int IDENTITY(1,1) not null,RId int not null references Requests,SId int not null references Services)
现在声明传递给存储过程的TVP是什么(注意这个脚本和下一个脚本需要在这个模拟中一起运行):
declare @NewValues table (
RequestName varchar(10) not null,
ServiceName varchar(10) not null
)
insert into @NewValues (RequestName,ServiceName) values
('R1','S1'),
('R1','S2'),
('R1','S3'),
('R2','S4'),
('R2','S5'),
('R3','S6')
然后,在 SP 内部,您将拥有如下代码:
declare @TmpRIDs table (RequestName varchar(10) not null,RId int not null)
declare @TmpSIDs table (ServiceName varchar(10) not null,SId int not null)
;merge into Requests r using (select distinct RequestName from @NewValues) n on 1=0
when not matched then insert (RequestName) values (n.RequestName)
output n.RequestName,inserted.RId into @TmpRIDs;
;merge into Services s using (select distinct ServiceName from @NewValues) n on 1=0
when not matched then insert (ServiceName) values (n.ServiceName)
output n.ServiceName,inserted.SId into @TmpSIDs;
insert into Mappings (RId,SId)
select RId,SId
from @NewValues nv
inner join
@TmpRIds r
on
nv.RequestName = r.RequestName
inner join
@TmpSIDs s
on
nv.ServiceName = s.ServiceName;
并检查结果:
select * from Mappings
产生:
MId RId SId
----------- ----------- -----------
1 1 1
2 1 2
3 1 3
4 2 4
5 2 5
6 3 6
这与您在问题中的内容相似。
代码的棘手部分是(错误地)使用该MERGE
语句,以便能够从inserted
表(包含新生成的IDENTITY
值)和充当行源的表中捕获列。该语句的OUTPUT
子句只允许引用伪表,所以不能在这里使用。INSERT
inserted