PL/SQL笔记整理

第一章:ref游标
Ref游标 又称 “动态游标”
(1)、在运行时使不同的语句与之关联
(2)、Ref游标可以使用游标变量
.游标变量
a. 一种引用类型
b. 可以再运行时指向不同的存储位置
c. Close语句关闭游标并释放用于查询的资源
.游标变量的类型
a.具有约束的游标变量
- 也就是具有返回类型的游标变量 也称为”强游标”
b.无约束的游标变量
- 也就是没有返回类型的游标变量 也称为 “弱游标”

declare 
type Refcur is ref cursor; --声明引用游标类型 游标返回的类型没有限制
     varcur Refcur;--游标变量
     type rec is record(
          sno varchar2(30),
          sname varchar2(30)
          ); --存储游标查询到得结果
     arec rec;
     flag int:=0;
begin
flag :=&flag;
if flag=0 then
open varcur for select hs.student_no,hs.student_name from hand_student hs ;
elsif flag=1 then
open varcur for select ht.teacher_no,ht.teacher_name from hand_teacher ht ;
else
open varcur for select hc.course_no,hc.course_name from hand_course hc;
--弱类型游标对目标表没有限制,数据可以使来自任何表
end if;
/* --for循环不能用于Ref游标,因为它是自动打开游标
   for arec in varcur  loop
   DBMS_output.put_line('no='||arec.sno ||' name:'||arec.sname);
   end loop;*/ 
loop
exit when varcur%notfound;--如果没有查询到数据就退出
fetch varcur into arec;
DBMS_output.put_line('no='||arec.sno ||' name:'||arec.sname);
end loop;
close varcur;
end;

2、强游标类型

---强型游标
declare
type varcur is ref cursor return hand_student_core%rowtype;
rec varcur;---游标变量
arec hand_student_core%rowtype;---存储游标查询结果
flag int:=0;
begin 
flag:=&flag;
if flag=0 then 
   open rec for select * from hand_student_core where core<60;
elsif flag=1 then 
   open rec for select * from hand_student_core where core between 60 and 69 ;
elsif flag=2 then 
   open rec for select * from hand_student_core where core between 70 and 79 ;
elsif flag=3 then 
   open rec for select * from hand_student_core where core between 80 and 100 ;
end if;
loop
    exit when rec%notfound;
    fetch rec into  arec;
    dbms_output.put_line(arec.student_no||'  '||arec.course_no||' '||arec.core);
    end loop;
    close rec;
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值