EG1: 验证隐式游标的特性
set serveroutput on;-- 开启输出
declare
no test.id%type;-- 声明变量no 的数据类型同test 表的id 列一致
ename test.name%type;
begin
if sql%isopen then -- 判断游标是否打开
dbms_output.put_line(' cursor is openning');
else
dbms_output.put_line('cursor is closed');
end if;
if sql%notfound then – 判断游标是否指向有效的行
dbms_output.put_line('no value');
else
dbms_output.put_line(no||' '||ename);
end if;
dbms_output.put_line(sql%rowcount);
dbms_output.put_line('---------------');
select id ,name into no ,ename from test where cj=90;-- 隐式游标必-- 须使用INTO
dbms_output.put_line(sql%rowcount);
if sql%isopen then
dbms_output.put_line(' cursor is openning');
else
dbms_output.put_line('cursor is closed');
end if;
if sql%notfound then
dbms_output.put_line('no value');
else
dbms_output.put_line(no||' '||ename);
end if;
exception
when no_data_found then
dbms_output.put_line('no value');
when too_many_rows then
dbms_output.put_line('too many rows');
end;
执行结果为:
cursor is closed
--------------------
1
cursor is closed
1001 qbnajj
去掉where 条件时的结果:
cursor is closed
--------------------
too many rows
EG2: 使用%ROWTYPE
declare
rowtype test%rowtype;
begin
select * into rowtype from test where cj=90;
dbms_output.put_line(rowtype.name||' '||rowtype.cj);
exception
when no_data_found then
dbms_output.put_line('no value');
when too_many_rows then
dbms_output.put_line('too many rows');
end;
执行结果:
qbnajj 90
EG3: 自定义RECORD 数据类型
declare
type r_type is record(name test.name%type ,cj test.cj%type);
re_type r_type;
begin
select name,cj into re_type from test where cj=90;
dbms_output.put_line(re_type.name||' '||re_type.cj);
exception
when no_data_found then
dbms_output.put_line('no value');
when too_many_rows then
dbms_output.put_line('too many rows');
end;
/
执行结果:
qbnajj 90
2) 显示游标
声明语法:
DECLARE
CURSOR cur_name( 参数名 数据类型) is select_satatements ;-- 游标名后-- 可带参数
BEGIN
OPEN cur_name;
FETCH cur_name into [ 变量或者其他数据类型];
-- 可用循环语句迭代输出游标所指向的结果集.
CLOSE cur_name;
END;
显示游标特性:
- 显示游标由用户定义, 并由用户来管理游标, 可返回多行记录.
- 通常使用显示游标需要遵守以下规则
声明游标 -> 打开游标 -> 读取数据 -> 关闭游标
但由于FOR IN LOOP 语句包含OPEN,FETCH,CLOSE 操作所以不在此例.
- 查看游标属性时, 使用cur_name%.
EG:PL/SQL 演示
declare
no test.id%type;
ename test.name%type;
cjs test.cj%type;
cursor test_cur is
select id,name from test where cj>=cjs;-- 可通过PL/SQL 绑定变量输-- 入值(&cjs)
begin
cjs:=50;
for t_cur in test_cur
loop
no:=t_cur.id;
ename:=t_cur.name;
dbms_output.put_line(no||' '||ename);
end loop;
end;
执行结果:
chenjunhua 80
qbnajj 90
cjh 52
EG2: 利用带参数的存储过程演示
create or replace procedure test_proce(cjs in test.cj%type)
as
no test.id%type;
ename test.name%type;
cursor test_cur is
select id,name from test where cj>=cjs;
begin
open test_cur;
fetch test_cur into no,ename;
while test_cur%found
loop
dbms_output.put_line(no||' '||ename);
fetch test_cur into no,ename;-- 将游标指向下条记录, 否则为死循环.
end loop;
close test_cur;
end test_proce;
exec test_proce(50);
执行结果:
chenjunhua 80
qbnajj 90
cjh 52
EG3: 带参数的游标与上面的类似