标签:
1. for in loop形式
DECLARE
CURSOR c_sal IS SELECT employee_id, first_name || last_name ename, salary
FROM employees ;
BEGIN
--隐含打开游标
FOR v_sal IN c_sal LOOP
--隐含执行一个FETCH语句
DBMS_OUTPUT.PUT_LINE(to_char(v_sal.employee_id)||‘---‘|| v_sal.ename||‘---‘||to_char(v_sal.salary)) ;
--隐含监测c_sal%NOTFOUND
END LOOP;
--隐含关闭游标
END;
2.普通的游标循环
declare
--定义游标并且赋值(is 不能和cursor分开使用)
cursor
stus_cur
is
select
*
from
students;
--定义rowtype
cur_stu students%rowtype;
/*开始执行*/
begin
--开启游标
open
stus_cur;
--loop循环
loop
--循环条件
exit
when
stus_cur%notfound;
--游标值赋值到rowtype
fetch
stus_cur
into
cur_stu;
--输出
dbms_output.put_line(cur_stu.
name
);
--结束循环
end
loop;
--关闭游标
close
stus_cur;
/*结束执行*/
end
;
declare
cursor
myemp_cur
is
select
*
from
myemp;
type myemp_tab
is
table
of
myemp%rowtype;
myemp_rd myemp_tab;
begin
open
myemp_cur;
loop
fetch
myemp_cur bulk collect
into
myemp_rd limit 20;
for
i
in
1..myemp_rd.
count
loop
dbms_output.put_line(
‘姓名:‘
||myemp_rd(i).ename);
end
loop;
exit
when
myemp_cur%notfound;
end
loop;
end
;
标签:
原文地址:http://www.cnblogs.com/zgz21/p/5864298.html