1、没有找到记录. end if; if not sql%isopen then dbms_output.put_line(sql游标已经关闭 end if; commit;end;/* 显式游标 用户自定义的游标 一般使用步骤: 声明游标: cursor 游标名 is 查询语句 ; 打开游标: open 游标名; 提取游标: fetch 游标名 into 变量; 关闭游标: close 游标名*/declare cursor v_cs is select * from scott.emp; -声明游标 v_temp v_cs%rowtype; open v_cs; -打开游标 loop fetc
2、h v_cs into v_temp; -提取游标 dbms_output.put_line(v_temp.ename | |v_temp.hiredate); exit when v_cs%notfound; -如果游标没有记录退出循环 end loop; close v_cs; -关闭游标-游标属性说明 v_dept number; cursor v_cur is select * from scott.emp where deptno=v_dept; c_temp v_cur%rowtype; v_dept:=30; open v_cur;fetch前游标总记录数:|v_cur%rowc
3、ount); if(v_cur%isopen) then游标已经打开 loop fetch v_cur into c_temp; dbms_output.put(游标总记录数:|v_cur%rowcount| if(v_cur%found) then dbms_output.put_line(c_temp.ename|c_temp.sal);游标已经没有记录 exit; end loop; close v_cur;/*带参数的游标 声明: cursor 游标名(形参 数据类型.) is select. where .=形参; 使用: open 游标名(实参); v_input scott.em
4、p.deptno%type; v_name scott.emp.ename%type; v_sal scott.emp.sal%type; cursor c_cur(v_param number) is select ename,sal from scott.emp where deptno=v_param; v_input:=&部门编号; open c_cur(v_input); -传入实参 fetch c_cur into v_name,v_sal; exit when c_cur%notfound; dbms_output.put_line(v_name|v_sal); close c_
5、cur;用于delete/update的游标 声明时指定 for update of 列 nowait 加行级锁 of 列 如果涉及到多张表,默认会在所有表上加行共享锁,为了只在特定表上加行共享锁,需要在for update子句后带有of子句指定字段名 更新时 update .where current of 游标名declare cursor c_cur is select * from scott.emp for update ; v_temp emp%rowtype; open c_cur; fetch c_cur into v_temp; exit when c_cur%notfoun
6、d; if(v_temp.sal1000) then update scott.emp set sal=sal+1 where current of c_cur; close c_cur; for 循环游标 自动打开,关闭,自动获取记录 cursor cursor_name is select. for v_name in cursor_name(或select语句) loop.end loop;例1 cursor c_cur is select * from scott.emp;begin for v_temp in c_cur loop -v_temp局部变量,无需声明 dbms_outp
7、ut.put_line(v_temp.ename|v_temp.sal); end loop;例2 v_input varchar2(1); v_input:请选择 if v_input=a then for v_rec in (select * from emp) dbms_output.put_line(v_rec.empno|:|v_rec.ename|v_rec.hiredate|v_rec.sal); elsif v_input=b for v_rec in (select * from dept) dbms_output.put_line(v_rec.deptno|v_rec.dn
8、ame);please input. return;/* for 循环游标(带参数) * 声明游标: cursor cursor_name(形参列表) is select. for v_name in cursor_name(实参)*/ v_input emp.deptno%type; cursor c(dept number) is select * from emp where deptno=dept ; for v_rec in c(v_input) loop dbms_output.put_line(v_rec.ename|/* ref cursor游标*用于处理运行时动态执行的 SQ
9、L查询 即在open游标时动态指定sql语句声明ref cursor类型 :TYPE IS REF CURSOR RETURN ; 声明ref游标类型的游标变量: 变量名 游标名; 打开方式: open cursor_name for 查询语句*/声明强类型的 REF 游标:TYPE t_refcur IS REF CURSOR RETURN emp%ROWTYPE;V_cur t_refcur;声明弱类型的 REF 游标 (用于动态SQL) TYPE t_refcur IS REF CURSOR; type v_refcur is ref cursor; - -声明 ref cursor 类
10、型 refcur v_refcur; -声明 ref cursor 类型的游标变量 v_id number; v_name varchar2(20); v_selection varchar2(1):=upper(substr(请输入员工(E)或部门编号(D),1,1); begin if v_selection=E open refcur for select empno,ename from emp; -打开游标=员工信息= elsif v_selection=D open refcur for select deptno,dname from dept;=部门信息=请输入员工信息(E)或
11、部门信息(D) - 返回 fetch refcur into v_id,v_name;- 提取游标 while refcur%found loop#|v_id|v_name); close refcur; - 关闭游标/*动态SQL(使用ref cursor 返回多行记录)* 使用占位符 Open cursor_name for v_sql using 占位符对应的变量*/ v_cur v_refcur; v_emp emp%rowtype; v_salary number; v_sql varchar2(100); v_dno number; v_salary :请输入薪水标准 v_dno:请输入部门编号 v_sql:select * from emp where sal1 and deptno=:2 order by sal desc open v_cur for v_sql using v_salary,v_dno;薪水大于|v_salary |的员工: fetch v_cur into v_emp; exit when v_cur%notfound;姓名:|v_emp.ename| 薪水:|v_emp.sal);
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2