存储过程游标使用

1)从表中读取数据(查看表中一共有多少条数据)
方式一:
create or replace procedure calledproc is
a number(38);
cursor selec_cur is select A from test;–定义一个静态sql游标
begin
open selec_cur;
loop
fetch selec_cur into a; –每次循环取到一个变量输入到b中
exit when selec_cur%notfound; –处理例外情况,当游标未读取导数据时,即退出
DBMS_OUTPUT.PUT_line(a);
end loop;
close celec_cur;
end;
方式二:
create or replace procedure testproc is
cursor cur_test is select A from test;
v_testa test.A%type; –定义一个变量,用来读取test表中的a字段。
begin
open cur_type;
loop
fetch cur_test into v_testa;
exit when cur_test%notfouond ;
dbms_output.put_line(v_testa.A);
end loop;
end;
方式三:
create or replace procedure testproc is
cursor cur_test is select A from test;
begin
for rec_test in cur_test loop –rec_test直接使用,不需要定义,此处直接将cur_test当做一个结果集来处理
dbms_output.put_line(rec_test.A);
end loop;
end;
动态游标:
create or replace procedure testproc is
type cur_type is ref cursor;–定义一个动态游标类型cur_type
cur_policy cur_type;
sqlStr varchar2(2000);
rec_policy test.A%rowtype;
begin
sqlStr:=’select A from test’;
open cur_policy for sqlStr;
loop
fetch cur_policy into rec_policy.A;
exit when cur_policy%notfound;
dbms_output.put_line(rec_policy.A);

end loop;
close cur_policy;
end ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值