zoukankan      html  css  js  c++  java
  • Oracle REF Cursor 用法(转帖)

    Oracle 系列:REF Cursor

    在上文  Oracle 系列:Cursor  (参见:http://blog.csdn.net/qfs_v/archive/2008/05/06/2404794.aspx)中
     提到个思考:怎样让游标作为参数传递? 解决这个问题就需要用到 REF Cursor 。

    1,什么是 REF游标 ?
     动态关联结果集的临时对象。即在运行的时候动态决定执行查询。
     
    2,REF 游标 有什么作用?
     实现在程序间传递结果集的功能,利用REF CURSOR也可以实现BULK SQL,从而提高SQL性能。

    3,静态游标和REF 游标的区别是什么?
     ①静态游标是静态定义,REF 游标是动态关联;
     ②使用REF 游标需REF 游标变量。
     ③REF 游标能做为参数进行传递,而静态游标是不可能的。
     
    4,什么是REF 游标变量?
     REF游标变量是一种 引用 REF游标类型  的变量,指向动态关联的结果集。

    5,怎么使用  REF游标 ?
     ①声明REF 游标类型,确定REF 游标类型;
      ⑴强类型REF游标:指定retrun type,REF 游标变量的类型必须和return type一致。
       语法:Type   REF游标名   IS   Ref Cursor Return  结果集返回记录类型;
      ⑵弱类型REF游标:不指定return type,能和任何类型的CURSOR变量匹配,用于获取任何结果集。
       语法:Type   REF游标名   IS   Ref Cursor;

     ②声明Ref 游标类型变量;
      语法:变量名  已声明Ref 游标类型;
      
     ③打开REF游标,关联结果集 ;
      语法:Open   Ref 游标类型变量   For   查询语句返回结果集;
      
     ④获取记录,操作记录;
      语法:Fatch    REF游标名 InTo   临时记录类型变量或属性类型变量列表;
      
     ⑤关闭游标,完全释放资源;
      语法:Close   REF游标名;
     
     例子:强类型REF游标
     /*conn scott/tiger*/
     Declare
      Type MyRefCurA IS  REF CURSOR RETURN emp%RowType;
      Type MyRefCurB IS  REF CURSOR RETURN emp.ename%Type;
      vRefCurA  MyRefCurA;
      vRefCurB  MyRefCurB;
      vTempA  vRefCurA%RowType;
      vTempB  vRefCurB.ename%Type;
      
     Begin
      Open  vRefCurA  For Select  *  from   emp   Where  SAL > 2000;
      Loop
       Fatch  vRefCurA InTo  vTempA;
       Exit  When  vRefCurA%NotFound;
       DBMS_OUTPUT.PUT_LINE(vRefCurA%RowCount||'  '|| vTempA.eno||'  '||vTempA.ename ||'  '||vTempA.sal)
      End Loop;
      Close vRefCurA;
      
      DBMS_OUTPUT.PUT_LINE('-------------------------------------------------------------------------------------------------------');
      
      Open  vRefCurB  For Select  ename  from   emp   Where  SAL > 2000;
      Loop
       Fatch  vRefCurB InTo  vTempB;
       Exit  When  vRefCurB%NotFound;
       DBMS_OUTPUT.PUT_LINE(vRefCurB%RowCount||'  '||vTempB)
      End Loop;
      Close vRefCurB; 
      
      DBMS_OUTPUT.PUT_LINE('-------------------------------------------------------------------------------------------------------');   
      
      Open  vRefCurA  For Select  *  from   emp   Where  JOB = 'CLERK';
      Loop
       Fatch  vRefCurA InTo  vTempA;
       Exit  When  vRefCurA%NotFound;
       DBMS_OUTPUT.PUT_LINE(vRefCurA%RowCount||'  '|| vTempA.eno||'  '||vTempA.ename ||'  '||vTempA.sal)
      End Loop;
      Close vRefCurA;
     End;
     
     例子:弱类型REF游标
     /*conn scott/tiger*/
     Declare
      Type MyRefCur  IS  Ref  Cursor;
      vRefCur MyRefCur;
      vtemp  vRefCur%RowType;
     Begin
      Case(&n)
       When  1 Then Open vRefCur  For Select   *   from emp;
       When  2 Then Open vRefCur  For Select   *   from dept;
       Else
        Open vRefCur  For Select   eno,  ename  from emp Where JOB = 'CLERK';
      End Case;
      Close  vRefCur;
     End;

    6,怎样让REF游标作为参数传递?

    --作为函数返回值
    create or replace function returnacursor return sys_refcursor
    is
       v_csr sys_refcursor;
    begin
        open v_csr for select a1 from test3;
        return v_csr;
    end;
    /

    declare
    c sys_refcursor;
    a1 char(2);
    begin
      c:=returnacursor;
      loop
        fetch c into a1;
        exit when c%notfound;
        dbms_output.put_line(a1);
      end loop;
      close c;
    end;
    /

    --作为参数
    create or replace procedure proc_ref_cursor (rc in sys_refcursor) as
      v_a number;
      v_b varchar2(10);
     
    begin
      loop
        fetch rc into v_a, v_b;
        exit when rc%notfound;
        dbms_output.put_line(v_a || ' ' || v_b);
      end loop;
    end;
    /

    declare
    v_rc sys_refcursor;
    begin
      open v_rc for
      select a1,a2 from test3;
      proc_ref_cursor(v_rc);
      close v_rc;
    end;
    /

  • 相关阅读:
    Linux Centos7(Mac) 安装Docker教程
    SpringBoot应用操作Rabbitmq(fanout广播高级操作)
    SpringBoot应用操作Rabbitmq(topic交换器高级操作)
    SpringBoot应用操作Rabbitmq(direct高级操作)
    CCF 字符串匹配(find()函数的使用)
    PKU 1204 Word Puzzles(AC自动机)
    PKU 1932 XYZZY(Floyd+Bellman||Spfa+Floyd)
    PKU 1201 Intervals(差分约束系统+Spfa)
    PKU 2352 Stars(裸一维树状数组)
    PKU 3169 Layout(差分约束系统+Bellman Ford)
  • 原文地址:https://www.cnblogs.com/honliv/p/2536252.html
Copyright © 2011-2022 走看看