是否有一种简单优雅的方法可以返回Oracle中两个无序分隔列表之间的差异?
示例:
差异:g3
答案 0 :(得分:3)
如果您有权访问APEX_UTIL
,则可以将字符串解析为数组,将它们转换为集合,然后使用MULTISET EXCEPT
(与MINUS相同但对于集合):
SET SERVEROUT ON
DECLARE
TYPE set_t IS TABLE OF varchar2(100);
listA APEX_APPLICATION_GLOBAL.vc_arr2;
listB APEX_APPLICATION_GLOBAL.vc_arr2;
excpt set_t;
FUNCTION to_set_t (arr IN APEX_APPLICATION_GLOBAL.vc_arr2)
RETURN set_t IS
rset set_t := set_t();
BEGIN
rset.EXTEND(arr.COUNT);
FOR i IN 1..arr.COUNT LOOP
rset(i) := TRIM(arr(i));
END LOOP;
RETURN rset;
END;
BEGIN
-- parse lists into arrays
listA := APEX_UTIL.string_to_table('a1, b4, g3, h6, t8, a0',',');
listB := APEX_UTIL.string_to_table('b4, h6, a0, t8, a1',',');
-- convert arrays to collections, then do the minus
excpt := to_set_t(listA) MULTISET EXCEPT to_set_t(listB);
-- display the results
FOR i IN 1..excpt.COUNT LOOP
DBMS_OUTPUT.put_line(excpt(i));
END LOOP;
END;
结果:
g3
有关MULTISET运算符的更多信息,这些运算符在10g中引入:http://www.oracle-developer.net/display.php?id=303