【问题标题】:Combine insert queries组合插入查询
【发布时间】:2015-11-18 12:44:25
【问题描述】:

我有以下使用同一个表的不同列的插入查询:

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 取决于我使用的列。有没有办法可以将所有这些组合成一个插入查询。

如果我不这样做,我必须一个一个地执行它,并且它会多次扫描表。

【问题讨论】:

    标签: sql oracle insert


    【解决方案1】:
    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';
    

    【讨论】:

      猜你喜欢
      • 2015-12-27
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 2012-07-16
      • 1970-01-01
      • 2016-03-30
      相关资源
      最近更新 更多