我创建了一个将 xml 数据解析为多个表的过程。我正在捕获主键约束的异常,如果在结果中发现重复项,它将被插入到名为 DUPLICATE 的表中。
现在,当我使用光标时,它的迭代次数往往超过所需的次数,即 1 次
。
DECLARE
PER_ID varchar2(20);
NAME varchar2(20);
SECTIONS_ID varchar2(20);
SECTIONS_NAME varchar2(20);
var1 number;
exception_var number;
CURSOR C1 IS
select d.department_id
, d.department_name
, s.sections_id
, s.sections_name
from xml_unit_download t
, xmltable(
'/ROWSET/DATA'
passing t.xml_file
columns
DEPARTMENT_ID varchar2(20) path 'DEPARTMENT/DEPARTMENT_ID'
, DEPARTMENT_NAME varchar2(30) path 'DEPARTMENT/DEPARTMENT_NAME'
, SECTIONS xmltype path 'SECTIONS'
) d
, xmltable(
'/SECTIONS'
passing d.sections
columns
SECTIONS_ID varchar2(20) path 'SECTIONS_ID'
, SECTIONS_NAME varchar2(30) path 'SECTIONS_NAME'
) s
where
t.Status = 4;
BEGIN
FOR R_C1 IN C1 LOOP
BEGIN
insert into DEPARTMENT(id, name) values(R_C1.PER_ID, R_C1.name);
insert into SECTIONS(id, name) values(R_C1.SECTIONS_ID, R_C1.SECTIONS_NAME);
var1:= var1+1;
dbms_output.put_line('Insert=' || var1);
commit;
--dbms_output.put_line('Duplicate='||var);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
dbms_output.put_line('Duplicate=');
insert into duplicate(id, name)values(R_C1.id, R_C1_name);
END;
END LOOP;
END;
我将如何处理这种情况?我也尝试过使用 INSERT ALL,但它似乎不起作用。这是我对 INSERT ALL 程序的尝试
DECLARE
PER_ID varchar2(20);
NAME varchar2(200);
var1 number;
exception_var number;
BEGIN
insert all
into SECTIONS (id) values(department_id)
--into sect (id, name) values(s.SECTIONS_ID, s.SECTIONS_NAME )
select d.department_id
, d.department_name
, s.sections_id
, s.sections_name
from xml_unit_download t
, xmltable(
'/ROWSET/DATA'
passing t.xml_file
columns
"DEPARTMENT_ID" varchar2(20) path 'DEPARTMENT/DEPARTMENT_ID'
, "DEPARTMENT_NAME" varchar2(30) path 'DEPARTMENT/DEPARTMENT_NAME'
, "SECTIONS" xmltype path 'SECTIONS'
) d
, xmltable(
'/SECTIONS'
passing d.sections
columns
"SECTIONS_ID" varchar2(20) path 'SECTIONS_ID'
, "SECTIONS_NAME" varchar2(30) path 'SECTIONS_NAME'
) s
where
t.Status = 4;
dbms_output.put_line('Insert=' || var1);
var1:= var1+1;
dbms_output.put_line('Insert=' || var1);
commit;
--dbms_output.put_line('Duplicate='||var);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
--insert into
dbms_output.put_line('Duplicate=');
END;
正在查询的 XML 包含 DEPARTMENT 及其 SECTIONS 的数据。DEPARTMENT 与 SECTIONS 有一对多的关系,即一个 DEPARTMENT 可以有一个或多个 SECTIONS,并且可能存在 DEPARTMENT 没有任何 SECTIONS 的情况。
XML 的结构是这样的,标签标识一个部门及其相应的部分。
XML
<ROWSET>
<DATA>
<DEPARTMENT>
<DEPARTMENT_ID>DEP1</DEPARTMENT_ID>
<DEPARTMENT_NAME>myDEPARTMENT1</DEPARTMENT_NAME>
</DEPARTMENT>
<SECTIONS>
<SECTIONS_ID>6390135666643567</SECTIONS_ID>
<SECTIONS_NAME>mySection1</SECTIONS_NAME>
</SECTIONS>
<SECTIONS>
<SECTIONS_ID>6390135666643567</SECTIONS_ID>
<SECTIONS_NAME>mySection2</SECTIONS_NAME>
</SECTIONS>
</DATA>
<DATA>
<DEPARTMENT>
<DEPARTMENT_ID>DEP2</DEPARTMENT_ID>
<DEPARTMENT_NAME>myDEPARTMENT2</DEPARTMENT_NAME>
</DEPARTMENT>
<SECTIONS>
<SECTIONS_ID>63902</SECTIONS_ID>
<SECTIONS_NAME>mySection1</SECTIONS_NAME>
</SECTIONS>
</DATA>
<DATA>
<DEPARTMENT>
<DEPARTMENT_ID>DEP3</DEPARTMENT_ID>
<DEPARTMENT_NAME>myDEPARTMENT3</DEPARTMENT_NAME>
</DEPARTMENT>
</DATA>
</ROWSET>