本期主题 灰蓝
游标用来处理从数据库中检索的多行记录(使用SELECT语句)存放的是select 的结果
利用游标,程序可以逐个地处理和遍历一次检索返回的整个记录集
--隐式游标
begin
update v_emp set ename='陈明羽' where empno = 7369;
if SQL%found then
dbms_output.put_line('隐式游标被找到');
end if;
end;
-- 显示游标被用于处理返回多行数据的SELECT 语句
-- 方式1
declare
v_row v_emp%rowtype;
--创建游标
cursor c_s is
select * from v_emp;
begin
--打开游标
open c_s;
loop
fetch c_s
into v_row;
exit when c_s%notfound;
dbms_output.put_line(v_row.ename);
dbms_output.put_line(v_row.sal);
end loop;
--关闭游标
close c_s;
end;
-- 方式2
declare
v_row v_emp%rowtype;
cursor cur is select * from v_emp;
begin
-- for循环 不用打开和关闭游标
for v_row in cur loop
dbms_output.put_line(v_row.ename);
dbms_output.put_line(v_row.sal);
end loop;
end;
-- 方式3
declare
v_row v_emp %rowtype;
cursor cur is
select * from v_emp;
begin
open cur;
--赋值到一个变量
fetch cur
into v_row;
while cur%found loop
dbms_output.put_line(v_row.ename);
dbms_output.put_line(v_row.sal);
fetch cur
into v_row;
end loop;
close cur;
end;
-- ref游标 动态sql查询 有return a_rows%rowtype的是强类型的
declare
type cur is ref cursor;
a_cur cur;
a_row v_emp%rowtype;
begin
open a_cur for
select * from v_emp;
fetch a_cur into a_row;
while a_cur%found loop
dbms_output.put_line(a_row.ename);
dbms_output.put_line(a_row.sal);
fetch a_cur
into a_row;
end loop;
close a_cur;
end;