2014-01-19 212 views
0
create or replace package demo_pckg 
as 
type cursor_type is ref cursor; 
procedure demo_proc(i_deptno number,o_result out cursor_type); 
end; 
/


create or replace package body demo_pckg 
as 
procedure demo_proc(i_deptno number,o_result out cursor_type) 
as 
begin 
    open o_result for 
    select * from employees 
    where department_id=i_deptno; 
    end; 
end; 
/

如果我想打印OUT遊標變量,應該怎麼辦?打印Oracle PLSQL OUT變量

+1

「印刷」是什麼意思?在sqlplus中顯示結果集?還有別的嗎? – Mat

回答

1

dbms_output.put_line打印到標準輸出。您需要啓用服務器輸出以查看結果:set serveroutput on。對應在您的環境一樣的sqlplus等進行 你需要選擇的結果到一個變量與select column1 into var1 from ...,這樣以後可以用dbms_output.put_line('my var1: ' || var1);

2

從SQL * Plus或SQL Developer中打印出來,最簡單的方式開啓服務器輸出與variableprint

var deptno number 
var result refcursor 
exec :deptno := 10; 
exec demo_pckg.demo_proc(:deptno, :result); 
print result 

或者:

var result refcursor 
declare 
    deptno emplyees.department_id%type;; 
begin 
    deptno := 10; 
    demo_pckg.demo_proc(deptno, :result); 
end; 
/
print result 

result被視爲一個綁定變量i在過程調用中,因此它的前綴爲:,但它是SQL * Plus的本地變量,因此它沒有用於調用print的變量。您可以在SQL * Plus中運行,也可以作爲SQL Developer中的腳本運行,它將在「腳本輸出」窗口中顯示結果。

無論是哪種情況,您都可以在過程調用中硬編碼deptno值,而不是使用變量來分隔變量。

如果您從Java或其他客戶端程序調用此方法,則可以像處理其他任何結果集那樣對待您的OUT遊標。

您也可以在程序中聲明resultsys_refcursor,而不是聲明自己的類型。