`
q272156430
  • 浏览: 270129 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

oracle 参数游标

阅读更多

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

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、使用游标更新数据

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、使用游标删除数据 

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后面跟字段名,如果跟表名或游标名称,则会报错:标示符无效。示例如下:

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,示例如下:

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循环,从而简化代码程序,示例如下:

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循环时直接使用子查询

declare   
begin   
for emp_record in (select * from cip_temps) loop   
dbms_output.put_line('第一行数据:'||emp_record.name||':'|| emp_record.age||':'||  emp_record.address||':'|| emp_record.id);   
end loop;   
end;  
 
分享到:
评论

相关推荐

    Oracle数据库游标使用大全

    Oracle数据库游标使用大全 Oracle数据库游标使用大全Oracle数据库游标使用大全

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

    用callabledStatement调用oracle存储过程实用例子(IN OUT 传参数包括游标类型)

    oracle 游标

    介绍oracle 中使用游标 游标在存储过程中的应用 (输入、输出参数) 例:通过输入部门号,查询某部门员工的姓名和工作。 (用输入参数和输出参数 LOOP循环)

    oracle游标溢出调优

    oracle游标溢出调优着眼点枚举。通过对相关数据字典和参数的分析,给出建议。

    OracleDataAccess游标参数.rar

    Oracle.DataAccess.dll ... Oracle.ManagedDataAccess.dll ...两者均可定义Oracle存过游标参数 OracleParameter ps_listcsr = new OracleParameter(); ps_listcsr.OracleDbType = OracleDbType.RefCursor;

    oracle 示例代码

    包括触发器、序列、游标参数 游标、转出过程参数、游标的rowcount用法 用游标修改数据 用for使用游标、视图、存储过程、隐式游标、过程等

    Oracle PL/SQL语言初级教程

    文档目录内容如下: Oracle PL/SQL语言初级教程 1 目录 1 ...带参数的游标 83 游标FOR循环 84 在游标FOR循环中使用查询 86 游标中的子查询 86 9.PL/SQL异常处理初步 88 异常传播 91 常用异常处理方法 93

    C#调用oracle方法(包括调用存储过程)

    详细的记录了C#如何调用oracle以及带有存储过程输出变量的方法,适合初学者。

    详解Hibernate呼叫Oracle的存贮过程和函数

    环境:Windows Server 2000、Red Hat Enterprise 5、Fedora、Oracle 9i/Oracle 10g/Oracle 11g、Windows XP Professional、MyEclipse 5.5 本示例演示Hibernate 3.2呼叫Oracle的存贮过程和函数,以及通过Hibernate的...

    Oracle数据库存储过程技术文档.doc

    2.2.4 带参数游标的使用方法 20 2.3 动态SQL语句 21 2.4 例外处理 22 2.5 一个完整的PL/SQL实例 24 第三章 oracle存储过程讨论 25 3.1 函数(FUNCTION) 26 3.1.1 用户函数创建,编译,删除 26 3.1.2 参数传递 27 ...

    Oracle存储过程返回游标实例详解

    有俩种方法: 一种是声明系统游标,一种是声明自定义游标,然后后面操作一样,参数类型为 in out 或out (1)声明个人系统游标.(推荐) 代码如下: create or replace p_temp_procedure ( cur_arg out sys_refcursor;...

    oracle 在一个存储过程中调用另一个返回游标的存储过程

    实际项目当中经常需要在一个存储过程中调用另一个存储过程返回的游标,本文列举了两种情况讲述具体的操作方法。

    21天学通Oracle

    第9章 游标(教学视频:36分钟) 162 第10章 触发器(教学视频:58分钟) 178 第11章 序列(教学视频:28分钟) 206 第12章 用户角色与权限控制(教学视频:45分钟) 215 第三篇 Oracle中的SQL 第13章 Oracle...

    Oracle11g从入门到精通2

    《Oracle11g从入门到精通》面向数据库管理人员和数据库开发人员,从实际角度出发,系统地介绍了数据库和Oracle的相关概念和原理、Oracle的数据库管理(如安装与启动,用户权限、备份与恢复等),以及Oracle的应用...

    ORACLE11G宝典.rar 是光盘里面的内容,书太厚咧没法影印啊

     《Oracle11g宝典》以Oracle提供的示例数据库为背景,从基本原理、理论提高、实际操作、经验策略、应用开发等方面,结合命令行方式、开发工具的使用、管理工具的使用、Oracle与Windows之间的关系等知识点,按照学习...

    Oracle9i的init.ora参数中文说明

    Oracle9i初始化参数中文说明 Blank_trimming: 说明: 如果值为TRUE, 即使源长度比目标长度 (SQL92 兼容) 更长, 也允许分配数据。 值范围: TRUE | FALSE 默认值: FALSE serializable: 说明: 确定查询是否获取表级...

    成绩分段oracle存储过程返回结果集

    oracle存储过程中,实现成绩分段显示人数,produce中带三个传入参数:起始分数(例如0),总分(例如100),分数间隔(例如10)。一个返回参数为游标,用来返回结果集。

    Oracle11g从入门到精通

    《Oracle11g从入门到精通》面向数据库管理人员和数据库开发人员,从实际角度出发,系统地介绍了数据库和Oracle的相关概念和原理、Oracle的数据库管理(如安装与启动,用户权限、备份与恢复等),以及Oracle的应用...

Global site tag (gtag.js) - Google Analytics