1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| --1、什么是游标 select * from scott.emp where deptno = 30 --表中都与行的数据
--2、游标的分类 --静态和引用 --静态游标:隐式和显式
--3、游标的属性
--4、游标的语法 --隐式语法 --不用打开,不用关闭,不用声明,它会在执行DML语句的时候,自动添加,当需要使用直接取值 --更新部门编号为30的所有员工工资加10%,返回受影响的行数 begin update scott.emp set sal = sal * 1.1 where deptno = 30; --使用隐式游标输出受影响的行数 dbms_output.put_line('修改成功条数:'|| SQL%Rowcount); end;
--显式语法 declare --1定义游标 cursor mycur is select empno,ename,sal from scott.emp where deptno =30; theempno number; thename varchar(20); thesal scott.emp.sal%TYPE; --使用和scott.emp.sal同样的数据类型 begin --2打开游标 open mycur; loop --3使用游标 fetch mycur into theempno,thename,thesal; exit when mycur%notfound; --退出循环的条件 dbms_output.put_line('编号:'||theempno||'姓名:'||thename||'工资:'||thesal); end loop; --4关闭游标 close mycur; end; --带参数的显式游标 declare --1定义游标(并且给游标带参数) cursor mycur(mydept number) is select empno,ename,sal from scott.emp where deptno = mydept; thedept number := 1; theempno number :=1; thename varchar(20); thesal scott.emp.sal%TYPE; --使用和scott.emp.sal同样的数据类型 begin thedept := &请输入一个部门编号; --2打开游标 open mycur(thedept); loop --3使用游标 fetch mycur into theempno,thename,thesal; exit when mycur%notfound; --退出循环的条件 dbms_output.put_line('编号:'||theempno||'姓名:'||thename||'工资:'||thesal); end loop; --4关闭游标 close mycur; end;
--循环游标 declare cursor mycur is select empno,ename,sal from scott.emp where deptno =30; begin for i in mycur loop dbms_output.put_line('编号:'||i.empno||'姓名:'||i.ename||'工资:'||i.sal); end loop; end;
|