Oracle 10g数据库游标的使用学习一

来源:互联网 发布:windows内核 编辑:程序博客网 时间:2024/05/24 04:21
--使用游标
1)9i以前的使用方法,一次取一条数据
--1、显示游标
declare--定义游标cursor temp_cursor isselect t.name,t.english_name from communitytype t;--where t.community_type_id = 'ebook';--定义变量v_name communitytype.name%type;v_english_name communitytype.english_name%type;begin--打开游标open temp_cursor;--循环取数据loop--使用fetch into语句取数据,每次只能取一行数据fetch temp_cursor into v_name,v_english_name;--当游标找不到数据时,退出exit when temp_cursor%notfound;dbms_output.put_line(v_name||':'||v_english_name);end loop;--关闭游标close temp_cursor;end;


    按需出版:test
    机构典藏:333
    电子图书:ebook
    学术期刊:ZJUPLink
    学位论文:ZJUDissertation
    课程教学:ZJUEducation
    收藏艺术品图鉴:
    Epub资源:
    专题订阅:ZJUSubjectSubscribe
    新书快读:newbook
    自助出版:wlcb
    图书馆新书:Library's New Book
    新出版图书:BookSeller New Book
    手机听书:tingbook
    人文社科:renwensheke
    测试资源库:test
    给对方:
    liangCC:
    0524teste:
    test01:
    团队资料库:Research Group
    0525test:
    test07:
    科教兴国:
    档案资源:archive
    多媒体教案:dmtja
    0524test:0524test
    qlltest:
    qlltest:
    05242:
    asd:asd
    ddd:ddd
    qq:qqq
    测试资源库:test001


用for循环访问游标中的记录时,可以不显示的打开或关闭游标,for循环回自动的执行这些操作
--用for循环访问游标中的记录时,可以不显示的打开或关闭游标,for循环回自动的执行这些操作declare--定义游标cursor temp_cursor isselect t.name,t.english_name from communitytype t;begin--循环取数据for v_comtype in temp_cursor loopdbms_output.put_line(v_comtype.name||','||v_comtype.english_name);end loop;end;


    初中多媒体教案,
    团队资料库,
    档案资源,
    按需出版,
    机构典藏,
    电子图书,ebook
    学术期刊,ZJUPLink
    学位论文,ZJUDissertation
    课程教学,ZJUEducation
    测试,test
    收藏艺术品图鉴,
    Epub资源,
    专题订阅,ZJUSubjectSubscribe
    新书快读,newbook
    投稿指南,
    图书馆新书,Library's New Book
    新出版图书,BookSeller New Book
    手机听书,tingbook
    人文社科,renwensheke
    测试,
    测试资源库,test
    给对方,
    书友会,
    自助出版,wlcb


2)采用集合一次取所有数据
--使用游标--1、显示游标declare--定义游标cursor temp_cursor isselect t.name from communitytype t;--定义嵌套表变量type name_table_type is table of communitytype.name%type;name_table name_table_type;begin--打开游标open temp_cursor;--使用bulk collect into语句取出全部数据fetch temp_cursor bulk collect into name_table;for i in 1..name_table.count loopdbms_output.put_line(name_table(i));end loop;--关闭游标close temp_cursor;end;


    按需出版
    机构典藏
    电子图书
    学术期刊
    学位论文
    课程教学
    收藏艺术品图鉴
    Epub资源
    专题订阅
    新书快读
    自助出版
    图书馆新书
    新出版图书
    手机听书
    人文社科
    测试资源库
    给对方
    liangCC
    0524teste
    test01
    团队资料库
    0525test
    test07
    科教兴国
    档案资源
    多媒体教案
    0524test
    qlltest
    qlltest
    05242
    asd
    ddd
    qq
    测试资源库

3)利用集合变量一次取部分数据
--1、显示游标declare--定义游标cursor temp_cursor isselect t.name from communitytype t;--定义变长数组变量type name_array_type is varray(5) of communitytype.name%type;name_array name_array_type;begin--打开游标open temp_cursor;--循环取数据loop--使用fetch into语句提取部分数据,每次取5个fetch temp_cursor bulk collect into name_array limit 5;dbms_output.put_line('资源库名称:');for i in 1..name_array.count loopdbms_output.put_line(name_array(i));end loop;dbms_output.new_line;--当游标找不到数据时,退出exit when temp_cursor%notfound;end loop;--关闭游标close temp_cursor;end;


    资源库名称:
    按需出版
    机构典藏
    电子图书
    学术期刊
    学位论文

    资源库名称:
    课程教学
    收藏艺术品图鉴
    Epub资源
    专题订阅
    新书快读

    资源库名称:
    自助出版
    图书馆新书
    新出版图书
    手机听书
    人文社科

    资源库名称:
    测试资源库
    给对方
    liangCC
    0524teste
    test01

    资源库名称:
    团队资料库
    0525test
    test07
    科教兴国
    档案资源

    资源库名称:
    多媒体教案
    0524test
    qlltest
    qlltest
    05242

    资源库名称:
    asd
    ddd
    qq
    测试资源库


4)、使用游标属性 isopen rowcount
--4、使用游标属性 isopen rowcountdeclare--定义游标cursor temp_cursor isselect t.name from communitytype t;--定义变量type name_table_type is table of communitytype.name%type;name_table name_table_type;begin--打开游标if not temp_cursor%isopen then open temp_cursor;end if;--取数据--使用fetch into语句提取部分数据,每次取5个fetch temp_cursor bulk collect into name_table;dbms_output.put_line('查询总行数:'||temp_cursor%rowcount);--关闭游标close temp_cursor;end;


    查询总行数:34

5)、基于游标定义记录变量
--5、基于游标定义记录变量declarecursor emp_cursor isselect ct.community_type_id,ct.name from communitytype ctwhere community_type_id = 'ebook';--定义基于游标的记录变量emp_record emp_cursor%rowtype;beginopen emp_cursor;loopfetch emp_cursor into emp_record;exit when emp_cursor%notfound;end loop;dbms_output.put_line(emp_record.name);  close emp_cursor;end;


    电子图书

6)使用有参数的游标
--使用有参数的游标,即指定游标从结果集中去取community_type_id为游标参数的记录declarecursor emp_cursor(id communitytype.community_type_id%type) isselect name from communitytypewhere community_type_id = id;v_name communitytype.name%type;beginopen emp_cursor('ebook');loopfetch emp_cursor into v_name;exit when emp_cursor%notfound;dbms_output.put_line(v_name); end loop;close emp_cursor;end;


    电子图书
0 0
原创粉丝点击