oracle開發學習篇之集合運算符以及集合異常捕獲

--取出集合;長度
declare
        type list_nested is table of varchar2(50) not null;
        v_all list_nested := list_nested('a','b','c','d','c','d');
begin
        dbms_output.put_line('list leng :' || cardinality(v_all));
end;
/

--從集合中取出取消重複的元素
declare
        type list_nested is table of varchar2(50) not null;
        v_all list_nested := list_nested('a','b','c','d','c','d');
begin
        dbms_output.put_line('list leng :' || cardinality((set(v_all))));
end;
/

--判斷集合是否爲空
declare 
    type list_nested is table of varchar2(50) not null;
    v_allA list_nested := list_nested('shanghai','beijing','changan');
    v_allB list_nested := list_nested('shanghai');
begin
    if v_allA is not empty then
        dbms_output.put_line('v_allA not null!');
    end if;
    if v_allB is empty then
        dbms_output.put_line('v_allB is null!');
    else
        dbms_output.put_line('v_allB not null!!');
    end if;
end;
/


--判斷字符是否存在
declare 
    type list_nested is table of varchar2(50) not null;
    v_allA list_nested := list_nested('shanghai','beijing','changan');
    v_allB list_nested := list_nested('shanghai');
    v_str  varchar2(20) := 'shanghai';
begin
    if v_str member of v_allA  then
        dbms_output.put_line('shanghai value is exists');
        end if;
end;
/(


--使用for循環遍歷集合的每個元素; 取出list中交集
declare
    type list_nested is table of varchar2(50) not null;
    v_allA list_nested := list_nested('shanghai','beijing','hunan');
    v_allB list_nested := list_nested('Java','beijing','tianjing');
    v_newlist         list_nested ;
BEGIN
    v_newlist := v_allA multiset except v_allB;
    for x in 1 .. v_newlist.count loop
        dbms_output.put_line(v_newlist(x));
    end loop;
end;
/

--使用for循環遍歷集合的每個元素; 取出集合中全部的元素
declare
    type list_nested is table of varchar2(50) not null;
    v_allA list_nested := list_nested('shanghai','beijing','hunan');
    v_allB list_nested := list_nested('Java','beijing','tianjing');
    v_newlist         list_nested ;
BEGIN
    v_newlist := v_allA multiset union v_allB;
    for x in 1 .. v_newlist.count loop
        dbms_output.put_line(v_newlist(x));
    end loop;
end;
/

判斷集合是否爲集合
declare
    type list_nested is table of varchar2(50) not null;
    v_allA list_nested := list_nested('shanghai','beijing','Java');
begin
    if v_allA is  A set then
        dbms_output.put_line('v_allA is list');
        end if;
end;
/

declare
    type list_nested is table of varchar2(50) not null;
    v_allA  varchar2(20) :=  'a';
begin
    if v_allA is  A set then
        dbms_output.put_line('v_allA is list');
        end if;
end;
/



--判斷B是否爲A的子集合
declare
    type list_nested is table of varchar2(50) not null;
    v_allA list_nested := list_nested('shanghai','beijing','hunan','Java');
    v_allB list_nested := list_nested('Java','beijing');
BEGIN
    if v_allB  submultiset v_allA then
        dbms_output.put_line('v_allB is v_allA submultiset');
    end if;
end;
/
--集合的異常處理;
--理解集合異常的纏身及處理操做; 全部異常捕獲都可以使用others進行捕獲;

DECLARE
        type list_varray is varray(8) of varchar2(50);
        v_info list_varray; --此時的集合變量沒有初始化
BEGIN
        v_info(0) := 10;  --此集合未初始化,因此會存在錯誤,
exception
        when collection_is_null then
        dbms_output.put_line('The error collection is not initialized');
END;
/




DECLARE
        type list_varray is varray(8) of varchar2(50);
        v_info list_varray := list_varray('shanghai','changan','facebook');    
BEGIN
        dbms_output.put_line(v_info(5));
exception
    when subscript_beyond_count then
        dbms_output.put_line('索引值超過定義的元素個數!!');
    end;
/



DECLARE
        type list_varray is varray(8) of varchar2(50);
        v_info list_varray := list_varray('shanghai','changan','facebook');    
BEGIN
        dbms_output.put_line(v_info('1'));
        dbms_output.put_line(v_info('a'));    
exception
    when value_error then
        dbms_output.put_line('索引值類型錯誤');
    end;
/



declare
        type info_index is table of varchar2(100) index by PLS_INTEGER;
        v_info info_index;
begin
        v_info(1) := 'fireof';
        v_info(2) := 'firefox.com';
        v_info(3) := 'www.firefox.com';
        v_info.delete(1);
        dbms_output.put_line(v_info(1));
        dbms_output.put_line(v_info(2));
        dbms_output.put_line(v_info(3));
exception
    when no_data_found then
        dbms_output.put_line('data not found !!!');
end;
/
相關文章
相關標籤/搜索