Oracle帶遊標的預存程序在plus中的調用執行個體

來源:互聯網
上載者:User

之前在文章  裡回答了一些網友的關於怎麼穿件一個返回記錄集合的預存程序。想必很多網友已經很明白了,這裡就不多講了。

怎麼調用含遊標的預存程序在sqlplus

Oracle怎麼執行帶遊標的過程?

給你一個例子

--遊標使用(遊標其實是一個放入記憶體暫存資料表)
declare
   money cms3_simcard.card_fee%type :=0; --定義與表欄位相同類型
   cursor mycursor is --定義遊標
          select * from cms3_simcard
          where return_flag = 1 and msisdn like '138%';
   my_record mycursor%rowtype;  --定義遊標記錄類型
   Counter int :=0;
  
begin
   open mycursor;  --開啟遊標
   if mycursor%isopen  then  --判斷開啟成功
   loop --迴圈擷取記錄集
     fetch mycursor into my_record; --擷取遊標中的記錄
         if mycursor%found then  --遊標的found屬性判斷是否有記錄
            dbms_output.put_line(my_record.card_fee);
         else
            exit;
         end if;
   end loop;
   else
     dbms_output.put_line('遊標沒有開啟');
   end if;
  close mycursor;
end;

如果你要問我程式裡怎麼調用 那你就不要問了 因為那個太多知道了 很少有人問到。 廢話不多說 上執行個體了

首先看下t1的表結構

  1. SQL> desc T1  
  2.  名稱                                      是否為空白? 類型  
  3.  ----------------------------------------- -------- ---------------------   
  4.   
  5.  D                                         NOT NULL DATE  
  6.  A                                                  NUMBER(38)  
  7.  B                                                  NUMBER(38)  
  8.  C                                                  NUMBER(38)  

看下T1的表裡的資料情況

  1. SQL> select * from t1;  
  2.   
  3. D                       A          B          C  
  4. -------------- ---------- ---------- ----------   
  5. 12-3月 -11            102         21         15  
  6. 14-3月 -11            100         58         73  
  7. 15-3月 -11            105                    87  

和上一個文章一樣 首先建立一個包先

  1. SQL> create or replace package pkg_package  
  2.   2  as  
  3.   3      type type_cursor is ref cursor;  
  4.   4      type type_record is record  
  5.   5      (  
  6.   6          test01 DATE,  
  7.   7          test02 NUMBER(38),  
  8.   8          test03 NUMBER(38) ,  
  9.   9          test04 NUMBER(38)  
  10.  10      );  
  11.  11  end;  
  12.  12  /  
  13.   
  14. 程式包已建立。  

建立一個帶遊標的的預存程序也就是一個返回記錄集合的預存程序

  1. SQL> create or replace procedure p_temp_procedure  
  2.   2  (  
  3.   3      cur_out_arg out pkg_package.type_cursor  
  4.   4  )  
  5.   5  is  
  6.   6  begin  
  7.   7      open cur_out_arg for select * from T1;  
  8.   8  end;  
  9.   9  /  
  10.   
  11. 過程已建立。  

該有的資料都有了,接著重點來了。  調用預存程序返回記錄集合

  1. SQL> declare  
  2.   2      cur_out_arg pkg_package.type_cursor;  
  3.   3      rec_arg pkg_package.type_record;  
  4.   4  begin  
  5.   5      dbms_output.put_line('------------------------');  
  6.   6      p_temp_procedure(cur_out_arg);  
  7.   7      loop  
  8.   8          fetch cur_out_arg into rec_arg;  
  9.   9         exit when cur_out_arg%notfound;  
  10.  10         dbms_output.put_line(rec_arg.test01||' '||rec_arg.test02||' '||rec_a  
  11. rg.test03||''||rec_arg.test04);  
  12.  11      end loop;  
  13.  12  end;  
  14.  13  /  
  15. ------------------------   
  16. 12-3月 -11 102 2115  
  17. 14-3月 -11 100 5873  
  18. 15-3月 -11 105 87  
  19.   
  20. PL/SQL 過程已成功完成。  
  21.   
  22. SQL>  

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.