insert into b..ks3 (wllx, wldm, kmdm)
select '3',dw,'131' from a..ksl where dw not in (select dw from b..ks2)
union all
select '3',dw,'151' from a..ksl where dw not in (select dw from b..ks2)
union all
select '3',dw,'113' from a..ksl where dw not in (select dw from b..ks2)
union all
select '3',dw,'211' from a..ksl where dw not in (select dw from b..ks2)
union all
select '3',dw,'231' from a..ksl where dw not in (select dw from b..ks2)

解决方案 »

  1.   

    我说漏了,首先要插入ks2表
    insert b..ks2(dw,wc,dc) select dw,dc,dc from a..ks1 where dw not in (select dw from b..ks2 )
    再插入ks3表,
    BEGIN TRAN 
    insert into b..ks3  (wllx, wldm, kmdm)values  ('3', @dw, '131')
    insert into b..ks3  (wllx, wldm, kmdm)values  ('3', @dw, '151')
    insert into b..ks3  (wllx, wldm, kmdm)values  ('3', @dw, '113')
    insert into b..ks3  (wllx, wldm, kmdm)values  ('3', @dw, '211')
    insert into b..ks3  (wllx, wldm, kmdm)values  ('3', @dw, '231')
    COMMIT TRAN 
    是同时做的,用not in 就不行了