oracle--游标--bai

时间:2023-03-10 00:22:41
oracle--游标--bai
--复制表
create table emp as(select * from scott.emp);
select * from emp;
--(1) 最简单的游标
declare --声明并初始化游标
cursor v_cur is
select empno,ename from emp order by empno;
v_empno emp.empno%type;
v_ename emp.ename%type;
begin
if(not v_cur%isopen) then --打开游标
open v_cur;
end if;
loop --提取记录
fetch v_cur into v_empno,v_ename;
if(v_cur%notfound) then --到达末尾,则退出循环
exit;
end if;
dbms_output.put_line('第'||v_cur%rowcount||'条记录:'||v_empno||','||v_ename);
end loop;
close v_cur; --关闭游标
end;
--(2) 带参数的游标
--查找指定部门的所有员工
cursor v_cur(v_deptno number) is
select empno,ename from scott.emp where deptno = v_deptno order by empno ;
v_empno scott.emp.empno%type;
v_ename scott.emp.ename%type;
v_dno scott.emp.deptno%type;
begin
v_dno:='&请输入部门编号:';
if(not v_cur%isopen) then
open v_cur(v_dno);
end if;
dbms_output.put_line('部门号'||v_dno||'的员工如下:');
loop
fetch v_cur into v_empno,v_ename;
if(v_cur%notfound) then --到达末尾,则退出循环
exit ;
end if;
dbms_output.put_line('第'||v_cur%rowcount||'条记录:'||v_empno||','||v_ename);
end loop;
--关闭游标
close v_cur;
exception
when others then
dbms_output.put_line('提取出错');
end; --(3) 可写游标。让每每个员工工资增加1000元
declare
--声明并初始化游标
cursor v_cur is
select 1 from scott.emp order by empno
for update of sal nowait ;
--v_sal scott.emp.sal%type;
i number;
begin
--打开游标
if(not v_cur%isopen) then
open v_cur;
end if;
--提取记录
loop
fetch v_cur into i;
if(v_cur%notfound) then --到达末尾,则退出循环
exit ;
end if;
--update scott.emp set sal = sal+10000 where empno = v_empno;
update scott.emp set sal = sal+10000 where current of v_cur;
end loop;
dbms_output.put_line('成功修改了'||v_cur%rowcount||'人的工资');
--关闭游标
close v_cur;
end; --(4) 简化方式1
declare
--声明并初始化游标
cursor v_cur is
select empno,ename from scott.emp order by empno;
begin
--提取记录
for rec in v_cur loop
dbms_output.put_line('第'||v_cur%rowcount||'条记录:'||rec.empno
||','||rec.ename);
end loop; end; --(5) 简化方式2
declare
--声明并初始化游标
begin
--提取记录
for rec in (select empno,ename from scott.emp order by empno)
loop
dbms_output.put_line(rec.empno
||','||rec.ename);
end loop; end; --(6) 返回游标的函数
--返回周三入职的员工
select * from scott.emp where to_char(hiredate,'day')='星期五'; create or replace function get_emps_hired_oneday
(
v_day varchar2
)return sys_refcursor
as
v_cur sys_refcursor;--声明1个局部变量游标,用于返回
begin
--打开指定结果集的游标.
open v_cur for
select ename,hiredate from scott.emp
where to_char(hiredate,'day')=v_day;
return v_cur; --将该记录集的游标返回
end; --调用函数,浏览全部的记录。 declare
v_cur sys_refcursor;
v_ename varchar2(20);
v_hiredate date;
begin --调用别人写好函数。
v_cur:= get_emps_hired_oneday('星期五'); --提取记录
loop
fetch v_cur into v_ename,v_hiredate;
if(v_cur%notfound) then --到达末尾,则退出循环
exit ;
end if;
dbms_output.put_line('第'||v_cur%rowcount||'条记录:'||v_ename||','
||to_char(v_hiredate,'yyyy-mm-dd day'));
end loop;
--关闭游标
close v_cur; end;