游标简单操作

 

 

--显示游标 处理检索多行数据

--while循环
declare cursor emp_cursor
is
select * from emp where deptno=&部门编号;
v_emp_row emp%rowtype;
v_i integer:=0;
begin
if not emp_cursor%isopen then
open emp_cursor;
end if;
fetch emp_cursor into v_emp_row;
while emp_cursor%found loop
dbms_output.put_line(v_emp_row.empno||' '||v_emp_row.ename||' ');
fetch emp_cursor into v_emp_row;
v_i:=v_i+1;
end loop;
dbms_output.put_line('利用游标一共处理了'||v_i||'行数据。。。');
if emp_cursor%isopen then
close emp_cursor;
end if;
end emp_cursor;

 

 

--简单loop循环(无参)
declare cursor emp_cursor
is
select empno 员工编号,ename 员工姓名,nvl(sal,0) 员工工资,nvl(comm,0) 月奖金,12*(nvl(sal,0)+nvl(comm,0)) 年收入
from emp
where deptno=&部门编号
order by empno asc;

v_emprec emp_cursor%rowtype;
begin

  if(not emp_cursor%isopen) then
   open emp_cursor;
  end if;
  fetch emp_cursor into v_emprec;
  
  loop
  exit when emp_cursor%notfound;
   dbms_output.put_line(v_emprec.员工编号);
   fetch emp_cursor into v_emprec;
  end loop;
  
  if(emp_cursor%isopen) then
   close emp_cursor;
  end if;
end;

 

 

--for循环(无参)
declare cursor emp_cursor
is
select empno 员工编号,ename 员工姓名,nvl(sal,0) 员工工资,nvl(comm,0) 月奖金,12*(nvl(sal,0)+nvl(comm,0)) 年收入
from emp
where deptno=&部门编号
order by empno asc;

begin  
  
  for i in emp_cursor loop
   dbms_output.put_line(i.员工编号);
  end loop;
  
end;

 

 

--带参数游标的使用

--for循环(带参)
declare cursor emp_cursor(p_empno emp.empno%type)
is
select empno 员工编号,ename 员工姓名,nvl(sal,0) 员工工资,nvl(comm,0) 月奖金,12*(nvl(sal,0)+nvl(comm,0)) 年收入
from emp
where deptno=&部门编号
order by empno asc;
begin

  for i in emp_cursor(&部门编号) loop
   dbms_output.put_line(i.员工编号);
  end loop;
  
end;


--while循环(带参)
declare cursor emp_cursor(p_empno emp.empno%type)
is
select empno 员工编号,ename 员工姓名,nvl(sal,0) 员工工资,nvl(comm,0) 月奖金,12*(nvl(sal,0)+nvl(comm,0)) 年收入
from emp
where deptno=&部门编号
order by empno asc;

v_emprec emp_cursor%rowtype;
begin
  if(not emp_cursor%isopen) then
   open emp_cursor(&部门编号);
  end if;
  fetch emp_cursor into v_emprec;
  
  while(emp_cursor%found) loop
   dbms_output.put_line(v_emprec.员工编号);
   fetch emp_cursor into v_emprec;
  end loop;
  
  if(emp_cursor%isopen) then
   close emp_cursor;
  end if;
end;


--简单循环(带参)
declare cursor emp_cursor(p_empno emp.empno%type)
is
select empno 员工编号,ename 员工姓名,nvl(sal,0) 员工工资,nvl(comm,0) 月奖金,12*(nvl(sal,0)+nvl(comm,0)) 年收入
from emp
where deptno=&部门编号
order by empno asc;

v_emprec emp_cursor%rowtype;
begin
  if(not emp_cursor%isopen) then
   open emp_cursor(&部门编号);
  end if;
  fetch emp_cursor into v_emprec;

  loop
  exit when emp_cursor%notfound;
   dbms_output.put_line(v_emprec.员工编号);
   fetch emp_cursor into v_emprec;
  end loop;
  
  if(emp_cursor%isopen) then
   close emp_cursor;
  end if;
end;

 

 

--隐式游标的使用

--按照给定的部门编号,删除该部门中的所有雇员信息
declare
v_input_deptno emp.deptno%type;
v_count number:=-1;
begin
v_input_deptno:=&部门编号;
select count(deptno) into v_count from emp
where deptno=v_input_deptno;
if(v_count>0) then
  delete from emp where deptno=v_input_deptno;
  --利用隐式游标的属性判断删除是否成功
  if(sql%found) then
  dbms_output.put_line('删除成功。。。');
  commit;
  else
  dbms_output.put_line('删除失败。。。');
  rollback;
  end if;
else
dbms_output.put_line('该部门不存在。。。');
end if;
end;

--更新游标 通常是为了进行更新操作,更新数据使用的游标
--按照给定的工种,更新雇员的工资,工资上调10%

declare cursor emp_cursor
is
select * from emp where job='&工种' for update of sal;
begin
for i in emp_cursor loop
update emp set sal=i.sal*1.1 where current of emp_cursor;
if(sql%found) then
dbms_output.put_line('更新成功。。。');
else
dbms_output.put_line('更新失败。。。');
end if;
end loop;
commit;
end;
--更新游标的for update 可以锁定游标正在处理的行记录,从而避免由于并发访问
--所带来的数据更新丢失问题。保证并发事务的有效处理
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值