随笔 - 493  文章 - 0  评论 - 97  阅读 - 239万

Oracle 存储过程返回结果集怎么这么费劲?

From: http://www.cnblogs.com/attraction/archive/2004/06/04/13489.aspx

 

存储过程返回记录集:
CREATE OR REPLACE PACKAGE pkg_test
AS
    TYPE myrctype IS REF CURSOR;
  
    PROCEDURE get (p_id NUMBER, p_rc OUT myrctype);
END pkg_test;
/
  
CREATE OR REPLACE PACKAGE BODY pkg_test
AS
    PROCEDURE get (p_id NUMBER, p_rc OUT myrctype)
    IS
       sqlstr   VARCHAR2 (500);
    BEGIN
       IF p_id = 0 THEN
          OPEN p_rc FOR
             SELECT ID, NAME, sex, address, postcode, birthday
               FROM student;
       ELSE
          sqlstr :=
             'select id,name,sex,address,postcode,birthday
            from student where id=:w_id';
          OPEN p_rc FOR sqlstr USING p_id;
       END IF;
    END get;
END pkg_test;

  
函数返回记录集:
建立带ref cursor定义的包和包体及函数:
CREATE OR REPLACE
package pkg_test as
/* 定义ref cursor类型
    不加return类型,为弱类型,允许动态sql查询,
    否则为强类型,无法使用动态sql查询;
*/
   type myrctype is ref cursor;  
   
--函数申明
   function get(intID number) return myrctype;
end pkg_test;
/
   
CREATE OR REPLACE
package body pkg_test as
--函数体
    function get(intID number) return myrctype is
      rc myrctype;  --定义ref cursor变量
      sqlstr varchar2(500);
    begin
      if intID=0 then
         --静态测试,直接用select语句直接返回结果
         open rc for select id,name,sex,address,postcode,birthday from student;
      else
         --动态sql赋值,用:w_id来申明该变量从外部获得
         sqlstr := 'select id,name,sex,address,postcode,birthday from student where id=:w_id';
         --动态测试,用sqlstr字符串返回结果,用using关键词传递参数
         open rc for sqlstr using intid;
      end if;
   
      return rc;
    end get;
   
end pkg_test;
/

posted on   清清飞扬  阅读(435)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
< 2010年11月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 1 2 3 4
5 6 7 8 9 10 11

点击右上角即可分享
微信分享提示