Oracle带游标的存储过程在plus中的调用实例
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的表结构
- SQL> desc T1
- 名称 是否为空? 类型
- ----------------------------------------- -------- ---------------------
- D NOT NULL DATE
- A NUMBER(38)
- B NUMBER(38)
- C NUMBER(38)
看下T1的表里的数据情况
- SQL> select * from t1;
- D A B C
- -------------- ---------- ---------- ----------
- 12-3月 -11 102 21 15
- 14-3月 -11 100 58 73
- 15-3月 -11 105 87
和上一个帖子一样 首先创建一个包先
- SQL> create or replace package pkg_package
- 2 as
- 3 type type_cursor is ref cursor;
- 4 type type_record is record
- 5 (
- 6 test01 DATE,
- 7 test02 NUMBER(38),
- 8 test03 NUMBER(38) ,
- 9 test04 NUMBER(38)
- 10 );
- 11 end;
- 12 /
- 程序包已创建。
创建一个带游标的的存储过程也就是一个返回记录集合的存储过程
- SQL> create or replace procedure p_temp_procedure
- 2 (
- 3 cur_out_arg out pkg_package.type_cursor
- 4 )
- 5 is
- 6 begin
- 7 open cur_out_arg for select * from T1;
- 8 end;
- 9 /
- 过程已创建。
该有的数据都有了,接着重点来了。 调用存储过程返回记录集合
- SQL> declare
- 2 cur_out_arg pkg_package.type_cursor;
- 3 rec_arg pkg_package.type_record;
- 4 begin
- 5 dbms_output.put_line('------------------------');
- 6 p_temp_procedure(cur_out_arg);
- 7 loop
- 8 fetch cur_out_arg into rec_arg;
- 9 exit when cur_out_arg%notfound;
- 10 dbms_output.put_line(rec_arg.test01||' '||rec_arg.test02||' '||rec_a
- rg.test03||''||rec_arg.test04);
- 11 end loop;
- 12 end;
- 13 /
- ------------------------
- 12-3月 -11 102 2115
- 14-3月 -11 100 5873
- 15-3月 -11 105 87
- PL/SQL 过程已成功完成。
- SQL>
评论暂时关闭