`
danwind
  • 浏览: 230038 次
  • 性别: Icon_minigender_1
  • 来自: 广东
社区版块
存档分类
最新评论

Oracle参数游标

 
阅读更多

一、参数游标
   参数游标是带有参数的游标,在定义参数游标之后,当使用不同参数值多次打开游标时,可以产生不同的结果集,语法如下:
cursor cursor_name(parameter_name datatype) is select_statement;
定义参数游标时,游标参数只能指定数据类型,而不能指定长度。
示例如下:

Oracle代码 复制代码 收藏代码
  1. declare   
  2. cursor temp_cursor(no number) is select name from cip_temps where id=no;   
  3. v_name cip_temps.name%type;    
  4. begin   
  5. open temp_cursor(1);   
  6. loop   
  7. fetch temp_cursor into v_name;   
  8. exit when temp_cursor%notfound;   
  9. dbms_output.put_line(v_name);   
  10. end loop;   
  11. close temp_cursor;   
  12. end;  
declare
cursor temp_cursor(no number) is select name from cip_temps where id=no;
v_name cip_temps.name%type; 
begin
open temp_cursor(1);
loop
fetch temp_cursor into v_name;
exit when temp_cursor%notfound;
dbms_output.put_line(v_name);
end loop;
close temp_cursor;
end;


二、使用游标更新或删除数据
通过使用显示游标,不仅可以一行一行的处理select语句结果,而且也可以更新或删除当前游标的数据,注意,如果要通过游标更新或删除数据,在定义游标时一定要带有for update子句,语法如下:
cursor cursor_name(parameter_name datatype) is select_statement for updae [of column_reference][nowait];如上所示:for update子句用于在游标结果集数据上加行共享锁,以防止其他用户在相应行上执行DML操作,当select语句要引用到多张表是,使用of子句可以确定哪些表要加锁,如果没有of子句,则会在select语句所引用的全部表上加锁,nowait用于指定不等待锁,为了更新或删除当前游标行数据,必须在update 或delete语句中引用where current of 子句,语法如下:
update table_name set column=.. where current of cursor_name;
delete from table_name  where current of cursor_name;

1、使用游标更新数据

Oracle代码 复制代码 收藏代码
  1. declare   
  2. cursor temp_cursor is select name,address,id from cip_temps for update;   
  3. v_name cip_temps.name%type;   
  4. v_address cip_temps.ADDRESS%type;   
  5. v_id cip_temps.id%type;   
  6. begin   
  7. open temp_cursor;   
  8. loop   
  9. fetch temp_cursor into v_name,v_address,v_id;   
  10. exit when temp_cursor%NOTFOUND;   
  11. if(v_id>4) then   
  12. update cip_temps set name='name'||to_char(v_id),address='address'||to_char(v_id) where current of temp_cursor;   
  13. end if;   
  14. end loop;   
  15. close temp_cursor;   
  16. end;  
declare
cursor temp_cursor is select name,address,id from cip_temps for update;
v_name cip_temps.name%type;
v_address cip_temps.ADDRESS%type;
v_id cip_temps.id%type;
begin
open temp_cursor;
loop
fetch temp_cursor into v_name,v_address,v_id;
exit when temp_cursor%NOTFOUND;
if(v_id>4) then
update cip_temps set name='name'||to_char(v_id),address='address'||to_char(v_id) where current of temp_cursor;
end if;
end loop;
close temp_cursor;
end;


2、使用游标删除数据

Oracle代码 复制代码 收藏代码
  1. declare   
  2. cursor temp_cursor is select name,address,id from cip_temps for update;   
  3. v_name cip_temps.name%type;   
  4. v_address cip_temps.ADDRESS%type;   
  5. v_id cip_temps.id%type;   
  6. begin   
  7. open temp_cursor;   
  8. loop   
  9. fetch temp_cursor into v_name,v_address,v_id;   
  10. exit when temp_cursor%NOTFOUND;   
  11. if(v_id>2) then   
  12. delete from cip_temps where current of temp_cursor;   
  13. end if;   
  14. end loop;   
  15. close temp_cursor;   
  16. end;  
declare
cursor temp_cursor is select name,address,id from cip_temps for update;
v_name cip_temps.name%type;
v_address cip_temps.ADDRESS%type;
v_id cip_temps.id%type;
begin
open temp_cursor;
loop
fetch temp_cursor into v_name,v_address,v_id;
exit when temp_cursor%NOTFOUND;
if(v_id>2) then
delete from cip_temps where current of temp_cursor;
end if;
end loop;
close temp_cursor;
end;


3、使用of子句在特定表加行共享锁。
如果使用子查询涉及到多张表,那么默认情况下会在所有表上加行共享锁,为了只在特定表上加行共享锁,需要在for update子句后带有of子句,of后面跟字段名,如果跟表名或游标名称,则会报错:标示符无效。示例如下:

Oracle代码 复制代码 收藏代码
  1. declare   
  2. cursor gData is select name,address,cip_temps.id from cip_temps,cip_t    
  3. where cip_temps.id=cip_t.id for update  of address;   
  4. rs gData%rowtype;   
  5. begin   
  6.   open gData;   
  7.   loop   
  8.      fetch gData into rs;   
  9.      exit when gData%notfound;   
  10.          if rs.id=1 then   
  11.             delete from cip_temps where current of gData;    
  12.          else   
  13.             update cip_temps set name='塞北的雪' where current of gData;   
  14.          end if;   
  15.      
  16.   end loop;   
  17.   close gData;   
  18. end;  
declare
cursor gData is select name,address,cip_temps.id from cip_temps,cip_t 
where cip_temps.id=cip_t.id for update  of address;
rs gData%rowtype;
begin
  open gData;
  loop
     fetch gData into rs;
     exit when gData%notfound;
         if rs.id=1 then
            delete from cip_temps where current of gData; 
         else
            update cip_temps set name='塞北的雪' where current of gData;
         end if;
  
  end loop;
  close gData;
end;


4、使用nowait子句
使用for update语句对被作用于行加锁,如果其他会话已经在被作用于行上加锁,那么默认情况下当前会话要一直等待对方释放锁,通过在for update子句中指定 nowait语句,可以避免等待锁,当指定了nowait子句之后,如果其他会话已经在被作用行加锁,那么当前会话会显示错误提示信息,并退出PL/SQL,示例如下:

Oracle代码 复制代码 收藏代码
  1. declare   
  2. cursor gData is select name,address,cip_temps.id from cip_temps,cip_t    
  3. where cip_temps.id=cip_t.id for update  nowait;   
  4. rs gData%rowtype;   
  5. begin   
  6.   open gData;   
  7.   loop   
  8.      fetch gData into rs;   
  9.      exit when gData%notfound;   
  10.          if rs.id=1 then   
  11.             delete from cip_temps where current of gData;    
  12.          else   
  13.             update cip_temps set name='塞北的雪' where current of gData;   
  14.          end if;   
  15.      
  16.   end loop;   
  17.   close gData;   
  18. end;  
declare
cursor gData is select name,address,cip_temps.id from cip_temps,cip_t 
where cip_temps.id=cip_t.id for update  nowait;
rs gData%rowtype;
begin
  open gData;
  loop
     fetch gData into rs;
     exit when gData%notfound;
         if rs.id=1 then
            delete from cip_temps where current of gData; 
         else
            update cip_temps set name='塞北的雪' where current of gData;
         end if;
  
  end loop;
  close gData;
end;


三、游标for循环
   使用游标for循环是循环游标最简单的方法,oracle会隐含打开游标、循环提取数据、关闭游标,语法如下:
     for record_name  in cursor_name  loop
           ..........
     end loop;
如上所示:cursor_name是已经定义的游标名称,record_name是oracle隐含定义的记录变量。
1、使用游标for循环
   当使用游标开发程序时,建议使用for循环,从而简化代码程序,示例如下:

Oracle代码 复制代码 收藏代码
  1. declare   
  2. cursor temp_cursor is  select name,age,address,id from cip_temps;   
  3. begin   
  4. for emp_record in temp_cursor loop   
  5. dbms_output.put_line(temp_cursor%rowcount||'第一行数据:'||emp_record.name||':'|| emp_record.age||':'||  emp_record.address||':'|| emp_record.id);   
  6. end loop;   
  7. end;  
declare
cursor temp_cursor is  select name,age,address,id from cip_temps;
begin
for emp_record in temp_cursor loop
dbms_output.put_line(temp_cursor%rowcount||'第一行数据:'||emp_record.name||':'|| emp_record.age||':'||  emp_record.address||':'|| emp_record.id);
end loop;
end;


2、在游标for循环时直接使用子查询

Oracle代码 复制代码 收藏代码
  1. declare   
  2. begin   
  3. for emp_record in (select * from cip_temps) loop   
  4. dbms_output.put_line('第一行数据:'||emp_record.name||':'|| emp_record.age||':'||  emp_record.address||':'|| emp_record.id);   
  5. end loop;   
  6. end;  

转载于http://shawnfree.iteye.com/blog/362128

分享到:
评论

相关推荐

    oracle 隐式游标,显示游标,游标循环

    - 在这里定义了一个名为`c_employees`的游标,其中`:dept_id`是参数。 ##### 3. 打开游标 ```sql OPEN c_employees USING 100; -- 假设部门ID为100 ``` ##### 4. 提取数据 ```sql DECLARE v_employee employees%...

    oracle数据库游标使用例子

    Oracle数据库中的游标是PL/SQL编程中一种重要的控制结构,它允许程序逐行处理查询结果,而不是一次性处理所有结果。游标对于处理大量数据或需要根据每行数据执行不同操作的情况非常有用。以下是关于Oracle游标使用的...

    oracle游标的总结oracle游标的总结

    Oracle 游标概述 Oracle 游标是 Oracle 数据库中的一种重要概念,用于查询数据库,获取记录集合(结果集)的指针。游标可以看作是一个临时表,你可以对其每一行的数据进行任意的操作。本文将对 Oracle 游标的概念、...

    Oracle数据库游标使用大全

    Oracle数据库中,游标是处理单条记录的重要工具,尤其在需要逐行处理查询结果集时。游标允许程序员在结果集中前进、后退,并访问每一行的数据。本篇文章主要探讨Oracle数据库中游标的使用。 首先,游标分为两种类型...

    非常详细的Oracle游标整理

    Oracle游标是数据库编程中非常重要的一个概念,主要用于处理SQL查询的结果集。游标允许我们按需逐行处理数据,而不是一次性加载所有结果。这里详细介绍了Oracle中的三种游标类型:隐式游标、显式游标和REF游标。 1....

    oracle函数触发器游标等几个小例子

    Oracle数据库是世界上最流行的数据库管理系统之一,它提供了丰富的功能来处理数据,包括函数、游标和触发器。在本文中,我们将深入探讨这些概念,并通过一些实际的例子来理解它们的用法。 1. **Oracle函数**:函数...

    ORACLE技术文档\oracle cursor 游标.doc

    Oracle数据库有几个与游标相关的初始化参数: 1. `OPEN_CURSORS`:这个参数定义了每个用户可以同时打开的最大游标数。当达到此限制时,会抛出`ORA-01000`错误。默认值可能因环境而异,但通常较小,如800。如果频繁...

    Oracle 游标使用大全

    - 游标可以作为参数传递,或者作为存储过程的输出结果。 9. **游标处理异常** - 应适当地处理游标相关的异常,例如NO_DATA_FOUND和TOO_MANY_ROWS。 10. **游标最佳实践** - 及时关闭游标以释放系统资源。 - ...

    oracle游标优化

    ### Oracle游标优化 在Oracle数据库管理中,游标是一种重要的机制,用于处理查询结果集。游标可以被看作是存储查询结果的一种临时区域,它允许用户通过循环逐行处理这些结果。游标不仅可以提高应用程序的灵活性,还...

    oracle游标使用及实例

    ### Oracle游标使用及实例详解 #### 一、Oracle游标概述 在Oracle数据库中,游标(Cursor)是一种用于处理SQL查询结果集的方式。它允许用户逐行地读取和处理查询结果,这对于需要对每一行数据进行特定操作的情况非常...

    用callabledStatement调用oracle存储过程实用例子(IN OUT 传游标)

    游标类型参数是 Oracle 存储过程中的一种特殊参数类型。游标类型参数可以实现数据的批量处理。在本示例中,我们使用游标类型参数来实现数据的批量处理。 五、示例代码解释 首先,我们创建了一个名为 test 的存储...

    Oracle的游标双机容错

    接着,定义了一个名为`testEptTbl`的存储过程包,其中包括一个游标`CUR_R0BrdLib`,用于从`tb_boardt`表中选择数据,以及一个`get_R0BrdLib`的存储过程,该过程接受批量大小、是否找到记录、是否完成获取等参数,...

    ORACLE中的游标汇总

    在示例代码中,`DECLARE`语句定义了一个名为`emp_cur`的游标,它接受一个参数`p_deptid`,然后通过`SELECT`语句获取`employees`表中`department_id`为`p_deptid`的所有列。`FETCH`语句将游标当前行的数据存储在变量`...

    oracle数据库 游标、存储过程和触发器.ppt

    Oracle 数据库游标、存储过程和触发器 Oracle 数据库是一种关系型数据库管理系统,它提供了多种机制来提高数据库的性能和安全性。在本文中,我们将讨论 Oracle 数据库中的三个重要概念:游标、存储过程和触发器。 ...

    ORACLE 游标 异常 存储过程

    在Oracle数据库中,游标(Cursor)是一种非常重要的概念,特别是在编写存储过程和函数时。游标允许我们处理单行或多行数据集,一次处理一行,这样可以进行精细化的数据操作。在本篇讨论中,我们将深入理解Oracle游标...

    oracle 游标 深入浅出 详解 精析 示例

    Oracle游标是数据库管理系统中的一种重要机制,它允许程序员逐行处理查询结果集,而不仅仅是一次性获取所有数据。游标类似于C语言中的指针,能够灵活、高效地处理多条记录,尤其在需要循环处理或者根据当前行数据做...

    JAVA调用ORACLE存储过程游标使用

    通过`registerOutParameter`方法注册这个参数,类型设置为`OracleTypes.CURSOR`,因为我们要接收的是Oracle的游标。执行`cs.execute()`后,我们可以从`cs.getObject(1)`获取到游标对象,将其转换为`ResultSet`,然后...

    快速练习ORACLE游标习题及答案

    根据提供的文件信息,我们可以归纳出以下Oracle游标的使用方法及相关知识点: ### 一、游标的基本概念 在Oracle数据库中,游标是一种重要的机制,它允许用户从查询结果集中逐行检索数据。游标可以分为两种类型:**...

    Oracle存储过程、游标、函数的详解

    ### Oracle存储过程、游标、函数的详解 #### 一、概述 在Oracle数据库中,存储过程、游标和函数是非常重要的组成部分,它们为数据库管理提供了强大的编程能力。通过学习这些概念,我们可以更加灵活地管理和操作...

    oracle 游标

    Oracle 游标是一种数据库编程工具,它允许程序员逐行处理查询结果集,而不仅仅是一次性获取所有数据。在Oracle中,游标尤其适用于处理大量数据,或者需要根据当前数据行进行决策的情况。游标通常与存储过程结合使用...

Global site tag (gtag.js) - Google Analytics