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;
/
时间: 2024-08-03 18:43:52

oracle开发学习篇之集合运算符以及集合异常捕获的相关文章

oracle开发学习篇之集合函数

集合函数; declare type list_nested is table of varchar2(100) not null; v_all list_nested := list_nested('changan','hubei','shanghai','beijing','Android','Java-Android'); BEGIN v_all.delete(1); for x in v_all.first .. v_all.last loop dbms_output.put_line(

C语言学习篇:逗号运算符的应用

今天来说说我最近碰到的一道C语言题!虽然说这个知识点应该有大神说过了,但是为了确保我确实掌握啦,还是决定将这个写出来! 先看看该题目的要求:对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止.请计算需要经过几步才能将n变到1,并且测试案例需要多组输入!(就直接说了吧,这个是九度oj里面的编号为1031的题目要求,大家有兴趣或者想刷题,感觉这是一个不错的选择) 首先看看我写的c语言代码(采用DEVc++编辑): 1 #include <std

Oracle DBA学习篇之SQL_TRACE

SQL_TRACE set linesize 10000; set pagesize 20000; set serveroutput on; alter session set sql_trace=true; select count(*) from firefox; alter session set sql_trace=false; --查看sql_trace trace file select * from v$diag_info where name like 'Default%'; s

程序员开发学习利器篇(下)之文档资料软件-获取软件、资料的最好途径

以下内容,开发初学者看,熟手略过. 论语有言: 工欲善其事 必先利其器 ,意思是工匠想要使他的工作做好,一定要先让工具锋利.比喻要做好一件事,准备工作非常重要. 第二篇我们来说说软件.文档资料.信息获取的高效途径. 首先说句题外话,程序员的软件.资料学习盘 应该是这个样子的: 文件夹视图应该是 详细列表 并且应用到电脑所有文件夹,方便查看文件的全名 最新的修改日期 类型 大小.并且文件显示应该是全名称 包括后缀名,文件目录起名字最好都是英文的,尤其是软件的安装目录,因为有些软件不识别中文的路径.

学习IOS开发UI篇--数据存储

iOS应用数据存储的常用方式 1.lXML属性列表(plist)归档 2.lPreference(偏好设置) 3.lNSKeyedArchiver归档(NSCoding) 4.lSQLite3 5.lCore Data Documents:保存应用运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录.例如,游戏应用可将游戏存档保存在该目录 tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除.应用没有运行时,系统也可能会清除该目录下的文件.iTunes同步设备时

学习IOS开发UI篇--UI知识点总结(四) UITabelView/UITableViewCell

UITabelView:常用属性 @property (nonatomic)          CGFloat    rowHeight;             // will return the default value if unset @property (nonatomic)          CGFloat     sectionHeaderHeight;   // will return the default value if unset @property (nonatom

学习IOS开发UI篇--UI知识点总结(三) UIScrollView/UIPageControl/NSTimer

UIScrollView:常用属性 @property(nonatomic)   UIEdgeInsets     contentInset;               // default UIEdgeInsetsZero. add additional scroll area around content @property(nonatomic,getter=isPagingEnabled) BOOL   pagingEnabled;     // default NO. if YES,

学习IOS开发项目篇--如何让程序在后台保持挂起状态

程序的状态分为:前台运行,后台挂起,后台休眠,为了让项目的网络请求保持活跃状态,需要对程序进行设置. 在applicationDidEnterBackground方法中调用下面的方法,可以让程序进入挂起状态,但在未知时间内,可能会被系统设置为休眠,如果在将程序设置为播放器,并且循环播放一个MP3文件,可以保持永久挂起状态. UIBackgroundTaskIdentifier task =[application beginBackgroundTaskWithExpirationHandler:

学习IOS开发UI篇--UITableView/数据模型嵌套/UITableViewCell/Cell的重用

1.UITableView ================================================== UITableView有两种格式:group和plain 2.UITableView如何展示数据 ================================================== UITableView需要一个数据源(dataSource)来显示数据 凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的