游标 cursor


a 定义游标——Cursor [ Cursor Name[param_name, param_type]] IS select xxx from xxxwhere xxx;

b 打开游标——Open [ Cursor Name[varialbe_value] ] ;

c 操作游标——Fetch [ Cursor Name ];

d 关闭游标——Close [ Cursor Name ] ;

常见显式Cursor用法:
1使用for循环来使用cursor:

create or replace procedure test_cursor is
cursor cur is select * from test_tb where id1 = 1;
userinfo test_tb%rowtype;
begin
for userinfo in cur loop
exit when cur%notfound;--退出循环当返回最后一条语句为假
dbms_output.put_line('user id : ' || userinfo.id1 || '-' ||
'user name : ' || userinfo.name1);
end loop;

exception
when others then
dbms_output.put_line(sqlerrm);
end test_cursor;
2使用fetch来使用cursor:

create or replace procedure test_fc
is
cursor cur is select * from test_tb where id1=1;
b test_tb%rowtype;
begin
open cur;
loop
exit when cur%notfound;
fetch cur into b;
dbms_output.put_line('user id : ' ||b.id1 || '-' ||
'user name : ' ||b.name1);

end loop;
exception
when others then
dbms_output.put_line(sqlerrm);
end test_cursor;

end test_fc;

原文地址:https://www.cnblogs.com/steel-chen/p/6889201.html