如何比较两个数组数据并列出缺失数据——collections exists方法
how to compare two array data and listing out missing data -- collections exists method
我有如下要求,有两个列表,与其他列表比较,找出列表中缺少的元素。在下面的例子中使用了 exist 方法,但是在执行这个之后我开始知道我们不能以这种方式使用 exist,它只能与索引一起使用。请指导我哪种方法可以帮助我满足此类要求
TABLE - MISSEVETEST(PID,DIR,EVE_NAME)
样本数据 - 111,O,CN.. 111,O,SN 输出:- RPI 是否缺失
CREATE OR REPLACE PROCEDURE EVE_MISS_POC IS
TYPE t_missing_eve IS TABLE OF MISSEVETEST%ROWTYPE;
list_missing_eve t_missing_eve := t_missing_eve();
TYPE t_out_eve IS VARRAY(3) OF VARCHAR2(80);
varray_out_eve t_out_eve;
CURSOR c_missing_eve is SELECT PID, DIR, EVE_NAME FROM MISSEVETEST;
current_pid PAY.PID%TYPE;
TYPE t_pid_present_eve IS VARRAY(5) OF VARCHAR2(80);
varray_pid_present_eve t_puid_present_eve := t_puid_present_eve();
BEGIN
DBMS_OUTPUT.PUT_LINE('PROCESS STARTRED');
varray_out_eve := t_out_eve('CN', 'SN', 'RPI');
OPEN c_missing_eve;
LOOP
-- processing payment records in batches. Each batch - 10000 records
FETCH c_missing_eve bulk collect INTO list_missing_eve limit 10000;
BEGIN
current_pid := list_missing_eve(1).PID; --assign first pid from list
FOR index_eve IN 1 .. list_missing_eve.count
LOOP
--this will collect all events belongs to same puid
IF current_pid = list_missing_eve(index_eve).PID THEN
varray_pid_present_eve.EXTEND;
-- collect all events of same puid
varray_pid_present_eve(varray_pid_present_eve.count) := list_missing_eve(index_eve).EVE_NAME;
ELSE
FOR i IN 1..varray_out_eve .COUNT LOOP
IF varray_pid_present_eve.EXISTS(varray_out_eve (i)) THEN
DBMS_OUTPUT.PUT_LINE('EVENT:' ||varray_out_eve (i) ||' EXIST');
ELSE
DBMS_OUTPUT.PUT_LINE('EVENT:' ||varray_out_eve (i) ||' NOT EXIST');
END IF;
END LOOP;
END IF;
END LOOP;
END;
EXIT WHEN c_missing_eve%notfound;
END LOOP; -- End for limit loop
CLOSE c_missing_eve;
END;
如果您使用 NESTED TABLE
而不是 VARRAY
,您可以使用 MULTISET EXCEPT
运算符。
SET SERVEROUTPUT ON
DECLARE
TYPE vtype IS TABLE OF VARCHAR2 (10);
v1 vtype
:= vtype ('ABC',
'DEF',
'GHI',
'KLM');
v2 vtype := vtype ('ABC', 'DEF');
v3 vtype := vtype ();
BEGIN
v3 := v1 MULTISET EXCEPT v2;
FOR i IN v3.FIRST .. v3.LAST
LOOP
DBMS_OUTPUT.PUT_LINE (v3 (i));
END LOOP;
END;
/
GHI
KLM
PL/SQL procedure successfully completed.
我有如下要求,有两个列表,与其他列表比较,找出列表中缺少的元素。在下面的例子中使用了 exist 方法,但是在执行这个之后我开始知道我们不能以这种方式使用 exist,它只能与索引一起使用。请指导我哪种方法可以帮助我满足此类要求 TABLE - MISSEVETEST(PID,DIR,EVE_NAME) 样本数据 - 111,O,CN.. 111,O,SN 输出:- RPI 是否缺失
CREATE OR REPLACE PROCEDURE EVE_MISS_POC IS
TYPE t_missing_eve IS TABLE OF MISSEVETEST%ROWTYPE;
list_missing_eve t_missing_eve := t_missing_eve();
TYPE t_out_eve IS VARRAY(3) OF VARCHAR2(80);
varray_out_eve t_out_eve;
CURSOR c_missing_eve is SELECT PID, DIR, EVE_NAME FROM MISSEVETEST;
current_pid PAY.PID%TYPE;
TYPE t_pid_present_eve IS VARRAY(5) OF VARCHAR2(80);
varray_pid_present_eve t_puid_present_eve := t_puid_present_eve();
BEGIN
DBMS_OUTPUT.PUT_LINE('PROCESS STARTRED');
varray_out_eve := t_out_eve('CN', 'SN', 'RPI');
OPEN c_missing_eve;
LOOP
-- processing payment records in batches. Each batch - 10000 records
FETCH c_missing_eve bulk collect INTO list_missing_eve limit 10000;
BEGIN
current_pid := list_missing_eve(1).PID; --assign first pid from list
FOR index_eve IN 1 .. list_missing_eve.count
LOOP
--this will collect all events belongs to same puid
IF current_pid = list_missing_eve(index_eve).PID THEN
varray_pid_present_eve.EXTEND;
-- collect all events of same puid
varray_pid_present_eve(varray_pid_present_eve.count) := list_missing_eve(index_eve).EVE_NAME;
ELSE
FOR i IN 1..varray_out_eve .COUNT LOOP
IF varray_pid_present_eve.EXISTS(varray_out_eve (i)) THEN
DBMS_OUTPUT.PUT_LINE('EVENT:' ||varray_out_eve (i) ||' EXIST');
ELSE
DBMS_OUTPUT.PUT_LINE('EVENT:' ||varray_out_eve (i) ||' NOT EXIST');
END IF;
END LOOP;
END IF;
END LOOP;
END;
EXIT WHEN c_missing_eve%notfound;
END LOOP; -- End for limit loop
CLOSE c_missing_eve;
END;
如果您使用 NESTED TABLE
而不是 VARRAY
,您可以使用 MULTISET EXCEPT
运算符。
SET SERVEROUTPUT ON
DECLARE
TYPE vtype IS TABLE OF VARCHAR2 (10);
v1 vtype
:= vtype ('ABC',
'DEF',
'GHI',
'KLM');
v2 vtype := vtype ('ABC', 'DEF');
v3 vtype := vtype ();
BEGIN
v3 := v1 MULTISET EXCEPT v2;
FOR i IN v3.FIRST .. v3.LAST
LOOP
DBMS_OUTPUT.PUT_LINE (v3 (i));
END LOOP;
END;
/
GHI
KLM
PL/SQL procedure successfully completed.