oracle--游标--bai
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | --复制表 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; |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步