189 8069 5689

oracle怎么控制游标 oracle的游标作用

oracle中如何定义一个游标?

1.游标定义:\x0d\x0acursor XXXA is\x0d\x0a SELECT 语句;\x0d\x0aXXXB cursorName%rowtype;\x0d\x0a\x0d\x0aXXXA: 游标名\x0d\x0aXXXB: 游标行数据定义\x0d\x0a\x0d\x0a2. 打开游标:\x0d\x0a-- 打开之前最好先关一下,防止上次发生异常没有关掉而引发不必要的异常\x0d\x0a IF XXXA%ISOPEN THEN\x0d\x0a CLOSE XXXA;\x0d\x0a END IF;\x0d\x0a\x0d\x0aOpen XXXA ;\x0d\x0a Loop\x0d\x0a Fetch XXXA into XXXB;\x0d\x0a exit when XXXA%NOTFOUND;\x0d\x0a... ... 处理逻辑\x0d\x0a end loop;\x0d\x0a close XXXA;

创新互联公司-专业网站定制、快速模板网站建设、高性价比湘西土家族网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式湘西土家族网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖湘西土家族地区。费用合理售后完善,十年实体公司更值得信赖。

Oracle 游标

游标能够根据查询条件从数据表中提取一组记录,将其作为一个临时表置于数据缓冲区中,利用指针逐行对记录数据进行操作。

Oracle中的游标分为显示游标和隐式游标 。

在执行SQL语句时,Oracle会自动创建隐式游标,该游标是内存中处理该语句的数据缓冲区,存储了执行SQL语句的结果。通过隐式游标属性可获知SQL语句的执行状态信息。

%found:布尔型属性,如果sql语句至少影响到一行数据,值为true,否则为false。

%notfound:布尔型属性,与%found相反。

%rowcount:数字型属性,返回受sql影响的行数。

%isopen:布尔型属性,当游标已经打开时返回true,游标关闭时则为false。

用户可以显式定义游标。使用显式游标处理数据要4个步骤:定义游标、打开游标、提取游标数据和关闭游标。

游标由游标名称和游标对应的select结果集组成。定义游标应该放在pl/sql程序块的声明部分。

语法格式:cursor 游标名称(参数) is 查询语句

打开游标时,游标会将符合条件的记录送入数据缓冲区,并将指针指向第一条记录。

语法格式:open 游标名称(参数);

将游标中的当前行数据赋给指定的变量或记录变量。

语法格式:fetch 游标名称 into 变量名;

游标一旦使用完毕,就应将其关闭,释放与游标相关联的资源。

语法格式:close 游标名称;

declare

cursor c1 is  select sno,cno,grade from sc;

v_sno sc.sno%type;

v_cno sc.cno%type;

v_grade sc.grade%type;

begin

open c1;

loop

  fetch c1 into v_sno,v_cno,v_grade;

  exit when c1%notfound;--紧跟fetch之后

if c1%found then

dbms_output.put_line(to_char(c1%rowcount)||v_cno);

end if;

end loop;

close c1; 

end;

declare

cursor c1 is select sno,cno,grade from sc;

v_sno sc.sno%type;

v_cno sc.cno%type;

v_grade sc.grade%type;

begin

open c1;

fetch c1 into v_sno,v_cno,v_grade;

while c1%found loop

  dbms_output.put_line(v_sno||v_cno||v_grade);

 fetch c1 into v_sno,v_cno,v_grade;

end loop;

close c1; 

end;

第三种:for

declare

cursor c1 is select sno,cno,grade from sc;

begin

for item in c1 loop

dbms_output.put_line(rpad(item.sno,'10',' ')||rpad(item.cno,'10',' ')||rpad(item.grade,'10',' '));

end loop;

end;

oracle中的游标使用静态游标

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

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

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

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

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

在PL/SQL中为所有的SQL数据操纵语句(包括返回一行的select)隐式声明游标 称为隐式游标 主要原因是用户不能直接命名和控制此类游标 当用户在PL/SQL 中使用数据操纵语句(DML)时 oracle预先定义一个名称为SQL的隐式游标 通过 检查隐式游标的属性获取与最近执行的SQL语句相关信息 在执行DML语句之后 隐式游标属性返回信息 隐式游标属性包括 %found %notfound %rowcount %isopen %found 只有DML语句影响一行或多行时 %found属性才返回true declare num number; begin update emp set empno= where empno= ; if sql%found then dbms_output put_line( 存在记录 ); else dbms_output put_line( 不存在记录 ); end if; end; %notfound %notfound属性作用正好跟%found属性相反 如果DML语句没有影响任何行数 则%notfound属性返回true declare begin delete from emp where empno= ; if sql%notfound then dbms_output put_line( 删除失败 ); end if; end; %rowcount %rowcount属性返回DML语句影响的行数 如果DML语句没有影响任何行数 则%rowcount属性将返回 declare num number; begin update emp set empno= where empno= ; if sql%rowcount= then dbms_output put_line( 不存在记录 ); else dbms_output put_line( 存在记录 ); end if; end; %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= ; if sql%rowcount= or sql%notfound then dbms_output put_line( 不存在记录 ); else dbms_output put_line( 存在记录 ); end if; end;

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

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

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

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

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

declare info emp%rowtype; cursor my_cur is select * from emp where empno= ; 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 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= 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循环之内

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

lishixinzhi/Article/program/Oracle/201311/18322


标题名称:oracle怎么控制游标 oracle的游标作用
URL网址:http://cdxtjz.cn/article/hjcsdh.html

其他资讯