oracle 在一個存儲過程中調用另一個返回遊標的存儲過程

        實際項目當中經常需要在一個存儲過程中調用另一個存儲過程返回的遊標,本文列舉了兩種情況講述具體的操作方法。

第一種情況:返回的遊標是某個具體的表或視圖的數據

create or replace procedure p_testa(presult out sys_refcursor) as
begin
  open presult for
    select * from users;
end p_testa;
其中 users 就是數據庫中一個表(或視圖)。在調用的時候只要聲明一個該表的rowtype類型就可以了:
create or replace procedure p_testb as
  temp_cur sys_refcursor;
  r        users%rowtype;
begin
  p_testa(temp_cur);
  loop
    fetch temp_cur
      into r;
    exit when temp_cur%notfound;
    dbms_output.put_line(r.name);
  end loop;
end p_testb;

第二種情況:我們返回的不是表的所有的列,或許只是其中一列或兩列 

create or replace procedure p_testa(presult out sys_refcursor) as
begin
  open presult for
    select id, name from users;
end p_testa;
這裏我們只返回了 users 表的 id, name 這兩個列,那麼調用的時候也必須做相應的修改: 
create or replace procedure p_testb as
  temp_cur sys_refcursor;
  cursor cur_1 is
    select id, name from users where rownum = 1;
  r cur_1%rowtype;
begin
  p_testa(temp_cur);
  loop
    fetch temp_cur
      into r;
    exit when temp_cur%notfound;
    dbms_output.put_line(r.id);
  end loop;
end p_testb;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章