Oracle带游标的存储过程在plus中的调用实例


之前在帖子  里回答了一些网友的关于怎么穿件一个返回记录集合的存储过程。想必很多网友已经很明白了,这里就不多讲了。

怎么调用含游标的存储过程在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>  

相关内容