比较 Collections
可以检查一个 collection 是否为 null。不能进行大于、小于等比较。这个约束也适用于隐式比较。例如,collection 不能出现在 DISTINCT、GROUP BY 或 ORDER BY 里。
若想进行比较操作,必须自定义比较函数。
示例 19:演示检查一个 collection 是否为 NULL
nested table 和 varray 可以自动为 null,所以可以用 NULL 来测试。
DECLARE TYPE emp_name_rec is RECORD (firstname employees.first_name%TYPE,
lastname employees.last_name%TYPE,
hiredate employees.hire_date%TYPE
);
TYPE staff IS TABLE OF emp_name_rec;
members staff;
BEGIN -- Condition yields TRUE because you have not used a constructor.IF members IS NULL THEN
DBMS_OUTPUT.PUT_LINE('NULL'); ELSE DBMS_OUTPUT.PUT_LINE('Not NULL');END IF;
END;/
示例 20:演示比较两个 Nested Tables
nested tables 可以进行等于或不等于比较。但是它们不是按顺序的,不存在大于或小于比较。
DECLARETYPE dnames_tab IS TABLE OF VARCHAR2(30);
dept_names1 dnames_tab :=
dnames_tab('Shipping','Sales','Finance','Payroll');
dept_names2 dnames_tab :=
dnames_tab('Sales','Finance','Shipping','Payroll');
dept_names3 dnames_tab :=
dnames_tab('Sales','Finance','Payroll');
BEGIN-- You can use = or !=, but not < or >.-- These 2 are equal even though members are in different order.IF dept_names1 = dept_names2 THEN
DBMS_OUTPUT.PUT_LINE
('dept_names1 and dept_names2 have the same members.');END IF;
IF dept_names2 != dept_names3 THEN
DBMS_OUTPUT.PUT_LINE
('dept_names2 and dept_names3 have different members.');END IF;
END;/
示例 21:演示用 Set 操作符比较 nested tables
可以使用 ANSI-standard 的 Set 操作符,检查一个 nested table 的某个属性,或比较两个 nested table。
DECLARETYPE nested_typ IS TABLE OF NUMBER;
nt1 nested_typ := nested_typ(1,2,3);
nt2 nested_typ := nested_typ(3,2,1);
nt3 nested_typ := nested_typ(2,3,1,3);
nt4 nested_typ := nested_typ(1,2,4);
answer BOOLEAN;howmany NUMBER;
PROCEDURE testify(truth BOOLEAN DEFAULT NULL
quantity NUMBER DEFAULT NULL) IS
BEGINIF truth IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE
(CASE truth WHEN TRUE THEN 'True' WHEN FALSE THEN 'False' END);
END IF;
IF quantity IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE(quantity);
END IF;
END;BEGINanswer := nt1 IN (nt2,nt3,nt4); -- true, nt1 matches nt2
testify(truth => answer);
answer := nt1 SUBMULTISET OF nt3; -- true, all elements match
testify(truth => answer);
answer := nt1 NOT SUBMULTISET OF nt4; -- also true
testify(truth => answer);
howmany := CARDINALITY(nt3); -- number of elements in nt3testify(quantity => howmany);
howmany := CARDINALITY(SET(nt3)); -- number of distinct elements
testify(quantity => howmany);
answer := 4 MEMBER OF nt1; -- false, no element matches
testify(truth => answer);
answer := nt3 IS A SET; -- false, nt3 has duplicates
testify(truth => answer);
answer := nt3 IS NOT A SET; -- true, nt3 has duplicates
testify(truth => answer);
answer := nt1 IS EMPTY; -- false, nt1 has some members
testify(truth => answer);
END;/
Collection 方法
http://www.linuxidc.com/Linux/2012-06/63711.htm
Collection 异常
http://www.linuxidc.com/Linux/2012-06/63717.htm
