使用Merge.输出来获得Sourcee.id和Target t.id之间的映射
declare @Source table (SourceID int identity(1,2), SourceName varchar(50))declare @Target table (TargetID int identity(2,2), TargetName varchar(50))insert into @Source values ('Row 1'), ('Row 2')
@Source
@Target
TargetID
SourceID
SourceChild
TargetChild
TargetID
TargetChild.TargetID
scope_identity()
TargetChild
.
@Target
SourceID
TargetID
TargetChild
.
SET IDENTITY_INSERT OFF
@Target
TargetChild.TargetID
.
output
insert into @Target(TargetName)output inserted.TargetID, S.SourceIDselect SourceNamefrom @Source as S
The multi-part identifier "S.SourceID" could not be bound.
merge @Target as Tusing @Source as Son 0=1when not matched then insert (TargetName) values (SourceName)output inserted.TargetID, S.SourceID;
TargetID SourceID----------- -----------2 14 3
from_table_name
列前缀,它指定DELETE、UPDATE或Merge语句的FROM子句中包含的表,用于指定要更新或删除的行。
蝴蝶不菲
相关分类