4 つのテーブルがあり、あるテーブルから次のテーブルに新しく挿入された pk を使用したいとします (これはすべて 1 つのバッチとして実行されます)。
declare @current_scope int;
insert into table1;
select t1.col;
set @current_scope = select SCOPE_IDENTITY(); --this pulls table1's new row id
insert into table2;
select t1.col, t2.col from table1 t1 join table2 t2 where t2.ProductID = @current_scope ;
set @current_scope = select SCOPE_IDENTITY(); --selects table2's new row
insert into table3;
select t2.col, t3.col from table2 t2 join table3 t3 where t3.ProductID = @current_scope ;
set @current_scope = select SCOPE_IDENTITY(); --selects table3's new row
insert into table4;
select t3.col, t4.col from table3 t3 join table4 t4 where t4.ProductID = @current_scope ;
これは可能ですか?
ありがとう