oracle学习之游标

--for 循环游标
declare
cursor my_cursor is select id,name,sex,age from t_user where rownum<10;
c_row my_cursor%rowtype;--游标行数据类型
begin
--c_row 完全可以直接用普通变量替代
for c_row in my_cursor loop
dbms_output.put_line(c_row.id||','||c_row.name||','||c_row.sex||','||c_row.age);
end loop;
end;

--fetch游标
--使用此游标必须明确的打开和关闭
declare
cursor my_cursor is select id,name,sex,age from t_user where rownum<20;
c_row my_cursor%rowtype;--游标行数据类型
begin
open my_cursor; --打开游标
loop
fetch my_cursor into c_row;
exit when my_cursor%notfound;
dbms_output.put_line(c_row.id||','||c_row.name||','||c_row.sex||','||c_row.age);
end loop;
end;

--fetch游标结合while
declare
cursor my_cursor is select id,name,sex,age from t_user where rownum<30;
c_row my_cursor%rowtype;--游标行数据类型
begin
open my_cursor; --打开游标
fetch my_cursor into c_row;
while my_cursor%found loop
dbms_output.put_line(c_row.id||','||c_row.name||','||c_row.sex||','||c_row.age);
fetch my_cursor into c_row;--继续获取下一行数据
end loop;
close my_cursor;
end;

--带参数的游标
declare
cursor my_cursor(p_area varchar2) is select id,name,sex,age from t_user where area=p_area;
c_row my_cursor%rowtype;--游标行数据类型
begin
for c_row in my_cursor('E017010') loop
dbms_output.put_line(c_row.id||','||c_row.name||','||c_row.sex||','||c_row.age);
end loop;
end;

--ref游标

declare
--type refcursor is ref cursor;--ref游标,弱类型
type refcursor is ref cursor return t_user%rowtype;--ref游标,强类型
user_cur refcursor;
c_row t_user%rowtype;
name t_user.name%type;
age t_user.age%type;
begin
open user_cur for select * from t_user where name like '王%' ;
loop
fetch user_cur into c_row;
exit when user_cur%notfound;
dbms_output.put_line(c_row.name||','||c_row.age);
end loop;
close user_cur;
end;

转载于:https://www.cnblogs.com/yong198707/p/7592350.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值