qindyyx 发表于 2013-1-14 09:05:51

游标简单操作

 
 
--显示游标 处理检索多行数据--while循环declare cursor emp_cursorisselect * from emp where deptno=&部门编号;v_emp_row emp%rowtype;v_i integer:=0;beginif not emp_cursor%isopen thenopen emp_cursor;end if;fetch emp_cursor into v_emp_row;while emp_cursor%found loopdbms_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 thenclose emp_cursor;end if;end emp_cursor;--简单loop循环(无参)declare cursor emp_cursorisselect empno 员工编号,ename 员工姓名,nvl(sal,0) 员工工资,nvl(comm,0) 月奖金,12*(nvl(sal,0)+nvl(comm,0)) 年收入from empwhere deptno=&部门编号order by empno asc;v_emprec emp_cursor%rowtype;beginif(not emp_cursor%isopen) then   open emp_cursor;end if;fetch emp_cursor into v_emprec;    loopexit 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_cursorisselect empno 员工编号,ename 员工姓名,nvl(sal,0) 员工工资,nvl(comm,0) 月奖金,12*(nvl(sal,0)+nvl(comm,0)) 年收入from empwhere 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)isselect empno 员工编号,ename 员工姓名,nvl(sal,0) 员工工资,nvl(comm,0) 月奖金,12*(nvl(sal,0)+nvl(comm,0)) 年收入from empwhere deptno=&部门编号order by empno asc;beginfor i in emp_cursor(&部门编号) loop   dbms_output.put_line(i.员工编号);end loop;end;--while循环(带参)declare cursor emp_cursor(p_empno emp.empno%type)isselect empno 员工编号,ename 员工姓名,nvl(sal,0) 员工工资,nvl(comm,0) 月奖金,12*(nvl(sal,0)+nvl(comm,0)) 年收入from empwhere deptno=&部门编号order by empno asc;v_emprec emp_cursor%rowtype;beginif(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)isselect empno 员工编号,ename 员工姓名,nvl(sal,0) 员工工资,nvl(comm,0) 月奖金,12*(nvl(sal,0)+nvl(comm,0)) 年收入from empwhere deptno=&部门编号order by empno asc;v_emprec emp_cursor%rowtype;beginif(not emp_cursor%isopen) then   open emp_cursor(&部门编号);end if;fetch emp_cursor into v_emprec;loopexit 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;--隐式游标的使用--按照给定的部门编号,删除该部门中的所有雇员信息declarev_input_deptno emp.deptno%type;v_count number:=-1;beginv_input_deptno:=&部门编号;select count(deptno) into v_count from empwhere deptno=v_input_deptno;if(v_count>0) thendelete from emp where deptno=v_input_deptno;--利用隐式游标的属性判断删除是否成功if(sql%found) thendbms_output.put_line('删除成功。。。');commit;elsedbms_output.put_line('删除失败。。。');rollback;end if;elsedbms_output.put_line('该部门不存在。。。');end if;end;--更新游标 通常是为了进行更新操作,更新数据使用的游标--按照给定的工种,更新雇员的工资,工资上调10%declare cursor emp_cursorisselect * from emp where job='&工种' for update of sal;beginfor i in emp_cursor loopupdate emp set sal=i.sal*1.1 where current of emp_cursor;if(sql%found) thendbms_output.put_line('更新成功。。。');elsedbms_output.put_line('更新失败。。。');end if;end loop;commit;end;--更新游标的for update 可以锁定游标正在处理的行记录,从而避免由于并发访问--所带来的数据更新丢失问题。保证并发事务的有效处理
页: [1]
查看完整版本: 游标简单操作