我創建了一個將xml數據解析爲多個表的過程。我正在捕獲主鍵約束的異常,並且如果在結果中找到重複項,它將被插入名爲DUPLICATE的表中。
現在,當我使用光標它往往比迭代所需要的次數更多,即1
程序光標for循環爲嵌套的XQUERY創建重複條目
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的嘗試所有程序
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沒有任何SECTION的情況。
XML的結構就是標籤標識一個DEPARTMENT和一組相應的SECTION。
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>
如果您在部門中有兩個部分,則您的查詢將返回兩行,具有相同的部門信息和不同的部分信息(您可以看到自己運行查詢)。因此,重複的部門是'有效的',並且不能區分一個部門的兩個部門,以及兩個相同的部門各有一個部門,如果這就是您想要做的。無論哪種方式,因爲您的重複部門得到異常,第二部分永遠不會插入。你可以把異常處理程序放在第一次插入,但這似乎不是最好的方法。 – 2013-05-04 11:00:53