组合插入查询
Combine insert queries
我有以下使用相同 table 的不同列的插入查询:
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 取决于我使用的列。有没有一种方法可以将所有这些组合成一个插入查询。
如果我不这样做,我必须一个接一个地执行它,它会多次扫描 table。
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';
我有以下使用相同 table 的不同列的插入查询:
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 取决于我使用的列。有没有一种方法可以将所有这些组合成一个插入查询。
如果我不这样做,我必须一个接一个地执行它,它会多次扫描 table。
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';