假设我有 4 个表,我想使用新插入的 pk 从一个表到下一个表(这都作为一批执行)
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 ;
这可能吗?
谢谢