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

--使用游标
1)9i以前的使用方法,一次取一条数据
--1、显示游标

declare
--定义游标
cursor temp_cursor is
select 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;

[list]
按需出版: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
[/list]

用for循环访问游标中的记录时,可以不显示的打开或关闭游标,for循环回自动的执行这些操作

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

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

2)采用集合一次取所有数据

--使用游标
--1、显示游标
declare
--定义游标
cursor temp_cursor is
select 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 loop
dbms_output.put_line(name_table(i));
end loop;
--关闭游标
close temp_cursor;
end;

[list]
按需出版
机构典藏
电子图书
学术期刊
学位论文
课程教学
收藏艺术品图鉴
Epub资源
专题订阅
新书快读
自助出版
图书馆新书
新出版图书
手机听书
人文社科
测试资源库
给对方
liangCC
0524teste
test01
团队资料库
0525test
test07
科教兴国
档案资源
多媒体教案
0524test
qlltest
qlltest
05242
asd
ddd
qq
测试资源库
[/list]
3)利用集合变量一次取部分数据

--1、显示游标
declare
--定义游标
cursor temp_cursor is
select 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 loop
dbms_output.put_line(name_array(i));
end loop;
dbms_output.new_line;
--当游标找不到数据时,退出
exit when temp_cursor%notfound;
end loop;
--关闭游标
close temp_cursor;
end;

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

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

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

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

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

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

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

[/list]
4)、使用游标属性 isopen rowcount

--4、使用游标属性 isopen rowcount
declare
--定义游标
cursor temp_cursor is
select 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;

[list]
查询总行数:34
[/list]
5)、基于游标定义记录变量

--5、基于游标定义记录变量
declare
cursor emp_cursor is
select ct.community_type_id,ct.name
from communitytype ct
where community_type_id = 'ebook';
--定义基于游标的记录变量
emp_record emp_cursor%rowtype;
begin
open emp_cursor;
loop
fetch emp_cursor into emp_record;
exit when emp_cursor%notfound;
end loop;
dbms_output.put_line(emp_record.name);
close emp_cursor;
end;

[list]
电子图书
[/list]
6)使用有参数的游标

--使用有参数的游标,即指定游标从结果集中去取community_type_id为游标参数的记录
declare
cursor emp_cursor(id communitytype.community_type_id%type) is
select name from communitytype
where community_type_id = id;
v_name communitytype.name%type;
begin
open emp_cursor('ebook');
loop
fetch emp_cursor into v_name;
exit when emp_cursor%notfound;
dbms_output.put_line(v_name);
end loop;
close emp_cursor;
end;

[list]
电子图书
[/list]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值