`
孔雀王子
  • 浏览: 40950 次
  • 性别: Icon_minigender_1
  • 来自: 河北
文章分类
社区版块
存档分类
最新评论

Oracle 10g数据库管理、应用与开发(二十)

阅读更多

23.2.7 游标变量

                        游标变量也可以处理多行查询结果集。游标变量的定义包括两个步骤:

1)定义cursor类型的指针

语法:

                                   Type ref_cursor_name is ref cursor[return return_type]

举例:

                                 Type var_cursor_name is ref cursor;

2)定义ref cursor类型的变量

                                   v_rc  var_cursor_name;

综合写法如下:

                                 Type var_cursor_name is ref cursor;

                                 v_rc  var_cursor_name;

上面的综合声明的游标变量称为弱ref cursor类型,因为它没有指明游标返回的结果,因此它可以指向任何一个具有多列的select查询结果。相对于上面还有一种称为强ref cursor类型。

声明方式如下:

                                 Type varcursorName is ref cursor return emp%rowtype; //指明了返回的结果

                                 Vcn varcursorName; //声明一个强的ref cursor类型的变量

使用游标变量与游标使用方式一样,也需要声明,打开,检索,关闭游标变量。

23.2.8 综合案例

SQL> declare
  			2  
  			3  type emp_cname is ref cursor return emp%rowtype;  //声明游标变量第一步
  			4  
 		 	5  ecname emp_cname;  //声明游标变量第二步
  			6  
  			7  emp_row emp%rowtype; //声明用于保存检索数据的变量
  			8  
  			9  begin
 			10      dbms_output.put_line('开始');
 	11      open ecname for select * from emp where empno=7934; //打开游标变量
 			12      loop
 			13       fetch ecname into emp_row; //查询结果赋值给保存的变量
 			14       exit when ecname%notfound;  //退出条件
 			15       dbms_output.put_line(emp_row.ename); //输出结果
 			16      end loop; //退出循环
 			17      close ecname;  //关闭游标变量
 			18    dbms_output.put_line('结束');
 			19  end;
 			20  /
开始
MILLER
结束
			
//复杂的案例(游标的循环遍历)
SQL> declare
  			2  
  			3  type emp_cname is ref cursor return emp%rowtype;
  			4  
  			5  ecname emp_cname;
  			6  
  			7  emp_row emp%rowtype;
  			8  
  			9  begin
 			10      dbms_output.put_line('开始');
 			11      open ecname for select * from emp;
 		12      loop
 			13       fetch ecname into emp_row;
 			14       exit when ecname%notfound;
 			15       dbms_output.put_line(emp_row.ename);
 			16      end loop;
 			17      close ecname;
 			18    dbms_output.put_line('结束');
 			19  end;
 			20  /
 			
开始
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
 			
结束

 

23.29 使用游标更新数据库

定位游标之后就可以进行删除(修改)指定的数据行。更新的时候需要使用for update选项,语法如下:

Cursor cursor_name is select_statement;

For update[of column[,column],[nowait]]

Of用来指定要锁定的列,如果忽略of那么表中选择的数据行都将锁定。如果被锁定行已经被锁定了,那么必须等待释放才能锁定对于这种情况我们可以使用nowait语句。

当使用for update语句声明游标后,可以再delete|update语句中使用where current of子句,修改|删除游标结果集中当前行对应的表中的数据。

                           语法如下:

                               Where { current of cursor_name|search_condition}

举例说明:

SQL> declare
  			2  
  			3   cursor ecname is select * from emp where empno=7934
  			4   for update of sal nowait;
  			5  
  			6   esal number(7,2);
  			7  
  			8  
  			9  begin
 			10      dbms_output.put_line('开始');
 			11  
 			12      for r in ecname loop
 			13      esal:=r.sal*10;
 			14      update emp set sal=esal where current of ecname;
 			15      end loop;
 			16  
 			17     dbms_output.put_line('结束');
 			18  end;
 			19  /
 			
开始
结束

  

0
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics