IT猫扑网:您身边最放心的安全下载站! 最新更新|软件分类|软件专题|手机版|论坛转贴|软件发布

您当前所在位置:首页数据库Oracle → oracle中的游标使用 静态游标

oracle中的游标使用 静态游标

时间:2015/6/28来源:IT猫扑网作者:网管联盟我要评论(0)

  游标是构建在PL/SQL中,用来查询数据,获取记录集的指针。它让开发者 一次访问结果集中一行记录。

  在oracle中提供了两种游标: 1 静态游标 2 ref游标

  静态游标:静态游标是在编译的时候就被确定。然后把结果集复制到内存中 静态游标又分为两种:隐式游标和显示游标。

  ref游标:ref游标是在运行的时候加载结果集

  先来看看静态游标中的隐式游标

  在PL/SQL中为所有的SQL数据操纵语句(包括返回一行的select)隐式声明游标 称为隐式游标。主要原因是用户不能直接命名和控制此类游标。当用户在PL/SQL 中使用数据操纵语句(DML)时,oracle预先定义一个名称为SQL的隐式游标,通过 检查隐式游标的属性获取与最近执行的SQL语句相关信息。 在执行DML语句之后,隐式游标属性返回信息。隐式游标属性包括: %found %notfound %rowcount %isopen 1 %found 只有DML语句影响一行或多行时,%found属性才返回true declare num number; begin update emp set empno=123 where empno=111; if sql%found then dbms_output.put_line('存在记录'); else dbms_output.put_line('不存在记录'); end if; end; 2 %notfound %notfound属性作用正好跟%found属性相反。如果DML语句没有影响任何行数 ,则%notfound属性返回true. declare begin delete from emp where empno=111; if sql%notfound then dbms_output.put_line('删除失败'); end if; end; 3 %rowcount %rowcount属性返回DML语句影响的行数。如果DML语句没有影响任何行数 ,则%rowcount属性将返回0。 declare num number; begin update emp set empno=123 where empno=111; if sql%rowcount=0 then dbms_output.put_line('不存在记录'); else dbms_output.put_line('存在记录'); end if; end; 4 %isopen %isopen属性判断SQL游标是否已经打开。在执行SQL语句之后,oracle自动关闭SQL 游标,所以隐式游标的%isopen属性始终为false. 在PL/SQL中向标准的select语句增加单独的into子句,就可以将从表或视图中查询的记录赋予变量或行变量。需要注意的是select ..into 语句结果必须有且只能有一行。 如果查询没有返回行,PL/SQL将抛出no_data_found异常。如果查询返回多行,则抛出 too_many_rows 异常。如果抛出异常,则停止执行,控制权转移到异常处理部分(没有 异常处理,则程序中断)。在引发异常时,将不使用属性%found,%notfound,%rowcount来查明DML语句是否 已影响了行数。 declare num number; begin select empno into num from emp where empno=111; if sql%rowcount=0 or sql%notfound then dbms_output.put_line('不存在记录'); else dbms_output.put_line('存在记录'); end if; end;

  ---------------------------------------------

  显示游标 显示游标是由用户显示声明的游标。根据在游标中定义的查询,查询返回的行集合可以 包含零行或多行,这些行称为活动集。游标将指向活动集中的当前行。 显示游标的操作过程。使用显示游标的4个步骤: (1)声明游标 (2)打开游标 (3)从游标中获取结果集 (4)关闭游标 cursor cursor_name [(parameter[,parameter])] [return return_type] is select_statement; cursor_name 指游标的名称。 parameter   为游标指定输入参数。 return_type 定义游标提取行的行类型。 select_statement 为游标定义查询语句。 open 游标名称 fetch 从游标中提取行 close 关闭游标

  1 打开游标,执行游标中定义的查询语句,绑定输入参数,将游标指针指 向结果集的BOF位置。 open cursor_name [parameters]

  2   fetch 在打开游标之后,可以从游标中提取记录 fetch cursor_name into variable_name; fetch 是提取结果集中一行记录存储在变量中。每次提取之后,结果集指针 就向前移动一行。

  3 close 在处理游标中的所有行之后,必须关闭游标,以释放分配给游标的所有资源。 close cursor_name 用户可以通过检查游标属性来确定游标的当前状态。

  显示游标的属性如下: %found:如果执行最后一条fetch语句,成功返回行,则%found属性为true。 %notfound:如果执行最后一条fetch语句,未能提取行,则%notfound属性为true。 %isopen:如果游标已经打开,则返回true,否则返回false。 %rowcount:返回到目前为止游标提取的行数。%rowcount为数字类型属性。在第一 次获取之前,%rowcount为零。当fetch语句返回一行时,则该数加1。

  declare info emp%rowtype; cursor my_cur is select * from emp where empno=111; begin open my_cur; dbms_output.put_line(my_cur%rowcount); loop if my_cur%isopen then fetch my_cur into info; exit when my_cur%notfound; dbms_output.put_line(info.empno); dbms_output.put_line(my_cur%rowcount); end if; end loop; close my_cur; end;

  ---------------------------------------------------

  使用显示游标删除或更新 使用游标时,如果处理过程中需要删除或更新。在定义游标查询语句时 必须使用select..for update语句,而在执行delete或update时使用 where current of 子句指定游标当前行。 cursor cursor_name is select_statement for update[of column] wait/nowait 在使用for update 子句声明游标之后,可以使用以下语法更新行 update table_name set column_name=column_value where current of cursor_name; update命令中使用的列必须出现在for update of 子句中 select 语句必须只包括一个表,而且delete和update语句只有在打开游标并且提取 特定行之后才能使用。

  declare cursor cur_emp is select * from emp where sal<2000 for update of sal; num emp%rowtype; begin open cur_emp; loop fetch cur_emp into num; exit when cur_emp%notfound; update emp set sal=2000 where current of cur_emp; end loop; close cur_emp; end;

  ---------------------------------------------------------------------------------------------------------------

  带参数的显示游标 PL/SQL中允许显示游标接受输入参数。用于声明带参数的显示游标语法 cursor cursor_name[<param_name> data_type] [return <return type>] is select_statement declare dept_num emp.deptno%type; emp_num   emp.empno%type; emp_nam emp.ename%type; cursor emp_cur(deptparam number) is select empno,ename from emp where deptno=deptparam; begin dept_num :=&部门编号; open emp_cur(dept_num); loop fetch emp_cur into emp_num,emp_nam; exit when emp_cur%notfound; dbms_output.put_line(emp_num||' '||emp_nam); end loop; close emp_cur; end;

  可以使用循环游标来简化显示游标。

  循环游标隐式打开显示游标(不需要open) 自动从结果集提取记录,然后处理完所有记录自动关闭游标。循环游标自动创建 %rowtype类型的变量并将此变量用做记录的索引。 循环游标语法如下: for record_index in cursor_name record_index是PL/SQL自动创建的变量,此变量的属性声明为%rowtype类型。作用 域for循环之内。

  循环游标的特性有: 从游标中提取所有记录之后自动关闭游标。

  提取和处理游标中每一条记录 提取记录之后%notfound属性为true则退出循环。如果未有结果集,则不进入循环。 declare cursor emp_cur is select * from emp; begin for temp in emp_cur loop dbms_output.put_line(temp.ename); end loop; end; 循环游标自动打开,提取,关闭。只适用于静态游标

关键词标签:oracle,静态游标

相关阅读

文章评论
发表评论

热门文章 Oracle中使用alter table来增加,删除,修改列Oracle中使用alter table来增加,删除,修改列oracle中使用SQL语句修改字段类型-oracle修oracle中使用SQL语句修改字段类型-oracle修使用低权限Oracle数据库账户得到管理员权限使用低权限Oracle数据库账户得到管理员权限Oracle对user的访问控制Oracle对user的访问控制

相关下载

人气排行 ORACLE SQL 判断字符串是否为数字的语句Oracle中使用alter table来增加,删除,修改列的语法ORACLE和SQL语法区别归纳(1)oracle grant 授权语句如何加速Oracle大批量数据处理Oracle删除表的几种方法ORACLE修改IP地址后如何能够使用Oracle 10g创建表空间和用户并指定权限