/**//*使用显示游标查询数据*/ set serveroutput on; declare sals emp_copy.sal%type; cursor mycur isselect sal from emp_copy where sal>2000; /**//*申明游标*/ begin open mycur; loop fetch mycur into sals; /**//*提取游标值赋予sals变量*/ exitwhen mycur%NOTFOUND;//**//*如果最后一条fetch语句没有提取到行数据,则%notfound的值为true*/ dbms_output.put_line(mycur%rowcount||'员工工资: '|| sals); end loop; close mycur;/**//*关闭游标*/ commit; end; /
/**//*显示游标更新行*/ set serveroutput on; declare sals emp_copy.sal%type; cursor mycur isselect sal from emp_copy where sal>2000forupdateof sal; begin open mycur; loop fetch mycur into sals; exitwhen mycur%notfound; update emp_copy set sal=sals*1.1wherecurrentof mycur;/**//*修改游标指向的行*/ end loop; close mycur; commit; end; /
/**//*带参数的游标*/ set serveroutput on; declare dno dept_copy.deptno%type; eno emp_copy.empno%type; e_name emp_copy.ename%type; cursor mysur(dtparam number) isselect empno,ename from emp_copy where deptno=dtparam;/**//*定义带参数的游标,注意:游标包含两个字段*/ begin dno :=&部门编号; open mysur(dno); /**//*打开带参数的游标*/ loop fetch mysur into eno,e_name;/**//*提取游标值到变量*/ exitwhen mysur%notfound; dbms_output.put_line(eno ||''|| e_name); end loop; close mysur; commit; end; /
c 循环游标:可以使用循环游标简化显示游标的代码。循环游标隐式打开游标,自动从活动集获取行,然后在处理完后自动关闭游标
/**//*循环游标:可以简化显示游标的处理代码。循环游标自动从活动集获取行,然后在处理完毕后自动关闭游标*/ set serveroutput on; declare cursor mycur isselect empno,ename,sal from emp_copy; begin for emp_rec in mycur /**//*emp_rec是记录索引*/ loop dbms_output.put_line('员工编号:'|| emp_rec.empno ||'员工姓名:'|| emp_rec.ename ||'员工工资:'|| emp_rec.sal); end loop; /**//*循环游标在处理完毕后自动关闭*/ end; /
2. REF游标:动态游标实例
/**//*REF游标: 引用游标,在程序运行时动态决定执行何种查询时,使用REF游标*/ set serveroutput on; declare type mycur is ref cursor;/**//*自定义类型mycur,是一个弱类型动态游标类型,因为没有返回参数*/ test_cur mycur; /**//*声明游标变量test_cur*/ t_id number; t_name varchar2(100); selection varchar2(1) :=upper(substr('&chr',1,1));/**//*取出输入的字符串的第一个字母,并将其变为大写字母。赋给声明的selection变量*/ begin if selection='E'then open test_cur forselect empno,ename from emp_copy; /**//*关联游标和结果集*/ dbms_output.put_line('员工信息:'); elsif selection='D'then open test_cur forselect deptno,dname from dept; dbms_output.put_line('部门信息'); else dbms_output.put_line('请输入正确的信息(E)或(D)'); return; endif; fetch test_cur into t_id,t_name; /**//*提取游标值给变量*/ while test_cur%found loop /**//*如果有数据行,则循环*/ dbms_output.put_line('#'|| t_id ||':'||t_name); fetch test_cur into t_id,t_name;/**//*提取下一条数据行*/ end loop; close test_cur; commit; end; /
/**//*REF游标:用REF游标演示动态SQL的使用*/ set serveroutput on; declare r_emp emp_copy%rowtype; /**//*定义一个emp_copy表的行类型变量*/ type mycur is ref cursor; /**//*声明一个REF游标类型*/ test_cur mycur; /**//*定义一个mycur游标类型的变量test_cur*/ r_sal number :=2500; begin /**//*关联游标到一个结果集,:1是占位符,表示可以引入或导出的第一个参数*/ open test_cur for'select * from emp_copy where sal>:1 order by sal desc' using r_sal; /**//*传入参数*/ dbms_output.put_line('工资大于'|| r_sal ||'的员工有:'); loop fetch test_cur into r_emp; /**//*将游标中的结果集第一行赋给行对象r_emp*/ exitwhen test_cur%notfound; dbms_output.put_line('编号:'|| r_emp.empno ||'姓名:'|| r_emp.ename ||'工资:'|| r_emp.sal); end loop; close test_cur; /**//*记得关闭游标*/ commit; end; /