--游标
游标的属性
%isopen boolean 如果游标打开,则为TRUE
%notfound boolean 如果最近的提取没有返回一条记录,则为TRUE
%found boolean 一直为TRUE,直到最近提取没有取回行记录
%rowcount boolean 到目前为止,提取的总行数
何云祥制作
--显示游标的使用方法
declare --声明
--定义变量
v_id test.id%type;
v_name test.name%type;
v_age test.age%type;
v_rowcount number; --存储提取总行数
cursor test_cur is
select id, name, age from test; --注意,游标声明的select语句中,不要包含INTO子句。
begin
open test_cur; --打开游标
fetch test_cur into v_id, v_name, v_age; --取值
while test_cur%found loop --或者使用while not test_cur%notfound loop
dbms_output.put_line(v_id||' '||v_name||' '||v_age); --输出
fetch test_cur into v_id, v_name, v_age;
end loop;
v_rowcount:=test_cur%rowcount; --使用%rowcount属性
dbms_output.put_line(v_rowcount);
close test_cur; --关闭游标
end;
--游标更新使用
declare
v_id employee.e_id%type;
v_name employee.e_name%type;
v_slary number(10);
cursor first_cur is select * from employee for update;
rec1 employee%rowtype;
begin
if(first_cur%isopen) then
null;
else
open first_cur;
end if;
fetch first_cur into rec1;
while first_cur%found loop
dbms_output.put_line(rec1.e_id||' '||rec1.e_name||' '||rec1.slary);
UPDATE employee set e_name='F-'||rec1.e_id WHERE CURRENT OF first_cur;
fetch first_cur into rec1;
end loop;
close first_cur;
end;