之前在文章 裡回答了一些網友的關於怎麼穿件一個返回記錄集合的預存程序。想必很多網友已經很明白了,這裡就不多講了。
怎麼調用含遊標的預存程序在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>