Oracle REF CURSOR-1

本文提供了多个使用Oracle REF CURSOR的示例代码,包括如何声明和使用REF CURSOR,从不同表格中获取数据,并展示了如何根据条件选择性地打开游标进行数据读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

展示使用后代的示例代码

======================================

示例#1

----------------------


declare
--declare the fer cursor.
type my_ref_cur_typ is ref cursor;
--declare a variable of rec cursor type.
my_ref_cur my_ref_cur_typ;
my_rec dept%rowtype;
my_rec1 emp%rowtype; 
begin
--open the cursor variable to select from a table.
   open my_ref_cur for select * from dept;
   loop
       fetch my_ref_cur into my_rec;
     dbms_output.put_line(my_rec.deptno||' '||
   my_rec.dname||' '||my_rec.loc); 
 exit when my_ref_cur%notfound;
    end loop;
--close the cursor.
close my_ref_cur; 
--open the same cursor to select from another set of ecords.
   open my_ref_cur for select empno,ename,job from emp;
   loop
       fetch my_ref_cur into my_rec1.empno,my_rec1.ename,my_rec1.job;
      dbms_output.put_line(my_rec1.empno||' '||
      my_rec1.ename||' '||my_rec1.job);
       exit when my_ref_cur%notfound;
    end loop;
close my_ref_cur; 
end; 
例子#2

----------------------


DECLARE
TYPE R1_CUR IS REF CURSOR;
MYVAR1 R1_CUR;
ename1 varchar(10);
sall number;
no number;
begin 
no:=&no; 
if no=10 then
open myvar1 for select sal from emp1 where empno=7900;
fetch myvar1 into  sall;
dbms_output.put_line(sall);
close myvar1;
else
open myvar1 for select ename from emp1 where empno=7900; 
fetch myvar1 into ename1; 
dbms_output.put_line(ename1); 
close myvar1;
end if;
end;  
例子#3

----------------------


declare
type ordertype is record(
orderno varchar2(5),
odate date,
vencode varchar2(5),
ostatus char(1),
del_date date); 
type ordercur is ref cursor return order_master%rowtype;
order_cv ordercur;
order_rec ordertype; 
begin 
open order_cv for select 
orderno,odate,vencode,ostatus,del_date from order_master
where vencode='v001';
loop
fetch order_cv into order_rec;
exit when order_cv%notfound;
dbms_output.put_line('The values are '||order_rec.orderno|| order_rec.odate ||' '||order_rec.vencode||' '||order_rec.ostatus||' '||order_rec.del_date); 
end loop;
close order_cv;
end; 
还要检查Oracle REF CURSOR-2

From: https://bytes.com/topic/oracle/insights/743232-oracle-ref-cursor-1-a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值