合并插入查询

时间:2015-11-18 12:44:25

标签: sql oracle insert

我使用相同的表使用不同的列进行以下插入查询:

insert into TABLE1(ID,RECORD_ID,V_ID,NUM,V_TYPE,V_Number,table,SP,row)
select '||V_SEQ||'.nextval,b.RECORD_ID,111,1,51,a.CMN,'TABLE3',''||SP||'',b.row
from TABLE2 B join TABLE3 A on B.row=A.row where B.table='TABLE3';
commit;

insert into TABLE1(ID,RECORD_ID,V_ID,NUM,V_TYPE,V_Number,table,SP,row)
select '||V_SEQ||'.nextval,b.RECORD_ID,112,1,51,a.CLN,'TABLE3',''||SP||'',b.row
from TABLE2 B join TABLE3 A on B.row=A.row where B.table='TABLE3';
commit;

insert into TABLE1(ID,RECORD_ID,V_ID,NUM,V_TYPE,V_Number,table,SP,row)
select '||V_SEQ||'.nextval,b.RECORD_ID,113,1,51,a.CLM_TYPE,'TABLE3',''||SP||'',b.row
from TABLE2 B join TABLE3 A on B.row=A.row where B.table='TABLE3';
commit;

insert into TABLE1(ID,RECORD_ID,V_ID,NUM,V_TYPE,V_Number,table,SP,row)
select '||V_SEQ||'.nextval,b.RECORD_ID,114,1,50,a.REV_CNTR,'TABLE3',''||SP||'',b.row
from TABLE2 B join TABLE3 A on B.row=A.row where B.table='TABLE3';
commit;

insert into TABLE1(ID,RECORD_ID,V_ID,NUM,V_TYPE,V_Number,table,SP,row)
select '||V_SEQ||'.nextval,b.RECORD_ID,115,1,50,a.HCPCS_CD,'TABLE3',''||SP||'',b.row
from TABLE2 B join TABLE3 A on B.row=A.row where B.table='TABLE3';
commit;

V_ID取决于我使用的列。有没有办法可以将所有内容组合成1个插入查询。

如果我不这样做,我必须逐个执行它并多次扫描表格。

1 个答案:

答案 0 :(得分:3)

insert into TABLE1(ID,RECORD_ID,V_ID,NUM,V_TYPE,V_Number,table,SP,row)
    select '||V_SEQ||'.nextval, b.RECORD_ID,
           x.col1, 1, x.col3,
           (case when x.col1 = 111 then a.CMN
                 when x.col1 = 112 then a.CLN
                 when x.col1 = 113 then a.CLM_TYPE
                 when x.col1 = 114 then a.REV_CNTR
                 when x.col1 = 115 then a.HCPCS_CD
            end), 
           'TABLE3', ''||SP||'', b.row
    from TABLE2 B join
         TABLE3 A
         on B.row = A.row CROSS JOIN
         ((select 111 as col1, 51 as col3 from dual) union all
          (select 112 as col1, 51 as col3 from dual) union all
          (select 113 as col1, 51 as col3 from dual) union all
          (select 114 as col1, 50 as col3 from dual) union all
          (select 115 as col1, 50 as col3 from dual)
         ) x         
    where B.table = 'TABLE3';