(转)oracle 存储过程 带游标作为OUT参数输出

存储过程返回OUT参数的游标 例子。

包中带过程 要自己定义一个type [cur_name] is ref cursor游标,返回的时候就直接 procedure AAA(变量名 out [cur_name])如此申明OUT变量

存储过程 用系统默认的 sys_refcursor 游标类型 定义变量就OK了

--=====================================================

Sql代码 :

--PL/SQL Code (包中带过程) 过程带游标的OUT参数,返回游标(ref cursor)
            
            create or replace package my_pack as
             type my_ref_cursor is ref cursor;
             procedure getMyCursor(val out my_ref_cursor);
            end my_pack;
            
            create or replace package body my_pack as
             procedure getMyCursor(val out my_ref_cursor)
             is
             begin
              open val for select * from student;
             end;
            end my_pack;
   --=====================================================

Sql代码 :
            --PL/SQL Code(存储过程) 带游标的OUT参数,返回游标(ref cursor)
            
            create or replace procedure retCursor(ret_cursor out sys_refcursor)is
            ret_cursor_value  sys_refcursor;
            begin
            open ret_cursor_value for select * from student;
             ret_cursor:=ret_cursor_value;
            end retCursor;

--=====================================================

下面是个每个学生求平均值的存储过程。遇到的问题是带参数游标中的变量名字不要和表中的一样,否则会出问题

create or replace procedure prc_student_avg_score
as

cursor c_sno is select s.sno , s.name from STUDENT s; --查询学生表的ID
cursor sc_avg(s_no varchar2) is select avg(sc.degree) from SCORE sc where sc.sno=s_no; --通过学生ID查询平均成绩

s_sno_j student.sno%type;   --变量ID
sc_avg_i score.degree%type; --变量平均成绩
sname   student.name%type;  
 
begin
 open c_sno;--打开查询ID的游标
 loop
   fetch c_sno into s_sno_j ,sname ;
   --DBMS_OUTPUT.PUT_LINE( '    c_sno当前扫描行:' || c_sno%rowcount );
   exit when c_sno%notfound;
     open sc_avg(s_sno_j); --打开查询平均成绩的游标,参数为学生ID
     loop
       fetch sc_avg into sc_avg_i;
       if  sc_avg_i is null or sc_avg_i=0  then
          sc_avg_i:=0 ;
       end if;
 
       exit when sc_avg%notfound;
       DBMS_OUTPUT.PUT_LINE(  sname || ':' ||sc_avg_i);
 
     end loop;
     close sc_avg;
 end loop;
 close c_sno;
end prc_student_avg_score;

(转)oracle 存储过程 带游标作为OUT参数输出的更多相关文章

  1. oracle存储过程和游标的使用

    oracle存储过程和游标的使用 (2011-04-19 14:52:47) 转载▼ 游标: 用来查询数据库,获取记录集合(结果集)的指针,我们所说的游标通常是指显式游标,因此从现在起没有特别指明的情 ...

  2. MySQL存储过程带in和out参数

    MySQL存储过程带in和out参数 最简单的例子: [html] mysql> DELIMITER $$ mysql> USE test $$ Database changed mysq ...

  3. Java调用oracle存储过程通过游标返回临时表数据

    注:本文来源于 <  Java调用oracle存储过程通过游标返回临时表数据   > Java调用oracle存储过程通过游标返回临时表数据 项目开发过程中,不可避免的会用到存储过程返回结 ...

  4. oracle存储过程(带参数的存储过程)

    带参数的存储过程 举例:为指定的员工涨100元的工资,打印涨前和涨后的工资 如果带参,需要指定是输入参数还是输出参数 create or replace procedure raisesalary(e ...

  5. Oracle存储过程、游标、函数

    SQL99是什么 (1)是操作所有关系型数据库的规则 (2)是第四代语言 (3)是一种结构化查询语言 (4)只需发出合法合理的命令,就有对应的结果显示 SQL的特点 (1)交互性强,非过程化 (2)数 ...

  6. myBatIs.Net 调用Oracle 存储过程返回游标

    找了好久,网上也没示例,全是java的,没办法,后来看到一个网上别人写的例子. http://www.myfirm.cn/blog/article/Control/13.html 上面照套还是出错,我 ...

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

    复制代码 代码如下:CREATE OR REPLACE PROCEDURE PROCSENDEMAIL(P_TXT VARCHAR2, P_SUB VARCHAR2, P_SENDOR VARCHAR ...

  8. Oracle存储过程 使用游标、数组的配合查询

    查询输入的门牌号码是否在标准门牌库中存在.存在则返回相应的号码. public string GetValidate(){ OracleConnection conn = ConnectOra(); ...

  9. oracle存储过程和游标参考

    oracle open cursor forhttp://www.itpub.net/thread-1874683-1-1.html

随机推荐

  1. 机器学习实战-K-nearest neighbors 算法的优缺点

    K临近算法是基于实例的学习,使用算法的时候我们必须要有接近分类结果的实例训练样本数据. 优点:精度高,对异常值不敏感 缺点: 时间复杂度和空间复杂度比较大.(如果训练样本数据集比较大,需要大量的空间来 ...

  2. Tomcat6性能优化

    1.内存设置      Windows环境下修改“%TOMCAT_HOME%\bin\catalina.bat”文件,在文件开头增加如下设置:set JAVA_OPTS=-Xms256m -Xmx51 ...

  3. Hadoop学习笔记:MapReduce框架详解

    开始聊mapreduce,mapreduce是hadoop的计算框架,我学hadoop是从hive开始入手,再到hdfs,当我学习hdfs时候,就感觉到hdfs和mapreduce关系的紧密.这个可能 ...

  4. 浙江理工2015.12校赛-F Landlocked

    Landlocked Time Limit: 5 Sec Memory Limit: 128 MB Submit: 288 Solved: 39 Description Canada is not a ...

  5. mac攻略(一) -- git使用

    1.首先在官网下载 https://git-scm.com/download/mac   2.然后安装git(mac自带git)   3设置Git的user name和email:   $ git c ...

  6. C# 4.0中dynamic的作用

    internal sealed class Coffee { public string GetName() { return "You selected Maxwell coffee.&q ...

  7. 使用clusterprofile做聚类分析

    library(clusterProfiler ) #cat test.txt gene_symbol EXOSC10ARHGEF10LVWA5B1SRRM1PTAFRCSMD2SH3GLB1GBP6 ...

  8. centos7 中libgdiplus的安装

    yum -y install libtool* git clone git://github.com/mono/libgdiplus.git cd libgdiplus ./autogen.sh -- ...

  9. 学习mongo系列(一) win/mac安装 解析 连接

    一.安装mongo数据库 下载链接https://www.mongodb.org/downloads, 在执行如下命令的时候事先按照目录新建如下的目录:(如果数据库安装在D盘就在D盘的根目录下建)&q ...

  10. hdu2243考研路茫茫——单词情结(ac+二分矩阵)

    链接 跟2778差不多,解决了那道题这道也不成问题如果做过基本的矩阵问题. 数比较大,需要用unsigned longlong 就不需要mod了 溢出就相当于取余 #include <iostr ...