2009-12-12 77 views
2

選擇不包含DML的Oracle存儲函數的返回值可以通過簡單地選擇函數來完成:如何在PLSQL Developer中測試包含DML的Oracle函數?

select function_name() from dual; 

如果函數包含DML(在這種情況下,一些插入來記錄傳遞給它的參數功能),不允許上述查詢。 (ORA-14551)

如何選擇/查看此功能的返回值?

如果我在plsql開發人員中選擇「test」,plsqldev會生成如下內容:

declare 
    -- Non-scalar parameters require additional processing 
    result xmltype; 
begin 
    -- Call the function 
    result := find_person(as_surname => :as_surname, 
         as_given => :as_given, 
         ad_birth_date_from => :ad_birth_date_from, 
         ad_birth_date_to => :ad_birth_date_to, 
         as_gender => :as_gender); 
end; 

如何查看「result」變量的值?開始/結束塊內的

select result from dual; 

生成

ORA-06550: PLS-00428: an INTO clause is expected in this SELECT statement 

回答

0

加入

pragma autonomous_transaction 

給函數在declare塊允許它被從雙

select find_person(arguments) from dual; 

選擇自t他在函數中的DML只是用來記錄傳遞給函數的參數,這是可以接受的使用autonomous_transaction,否則應該避免

+0

真的,你應該有一個被稱爲 – 2009-12-13 04:30:51

+0

的日誌程序使用autonomous_transaction進行日誌記錄通常是一個壞主意。你必須提交日誌,然後不能總是看到用戶實際上做了什麼,如果主事務回滾。 – jva 2009-12-14 10:53:00

+0

-1:這並不回答被問到的問題 – kurosch 2009-12-21 22:25:40

0

pragma_autonomous_transaction是一種方法。

但出影響您的原始數據庫來測試,有一些開源工具來測試像DBUnit的,utPLSQL等

您的SQL/PLSQLs這些單元測試工具SQL和PLSQL

0

的PLSQL開發人員的測試屏幕有兩個部分。在上面你會發現你在問題中顯示的代碼。測試函數生成的代碼已將函數的變量替換爲綁定變量::as_surname,:as_given等。 在屏幕的下半部分,可以輸入這些參數的值並查看結果的值。

1

我還沒有和XMLType的工作,但文檔提供了以下選項:

dbms_output.put_line(result.getStringVal()); 
2

變「結果」爲「:結果」,並在變量的左上角點擊小箭頭啄格。 它應該添加「結果」作爲綁定varibale,您可以指定其類型。

在你的情況下,最好的選擇是clob或PL/SQL字符串。

而且你的腳本可能看起來像這樣:

declare 
    result xmltype; 
begin 
    result := find_person(as_surname => :as_surname, 
         as_given => :as_given, 
         ad_birth_date_from => :ad_birth_date_from, 
         ad_birth_date_to => :ad_birth_date_to, 
         as_gender => :as_gender); 
    if result is null then 
    :result := null; 
    else 
    :result := result.GetClobVal(); 
    end if; 
end; 

正如你所看到的,它基本上是什麼PL/SQL開發已經爲您創建,除了如何在一個方法返回的XMLType處理PL/SQL Dev可以理解。

,如果你想返回一個結果,你可以返回光標:

begin 
    ... 
    open :someCursor for 
    select 1 from ...; 
    ... 

你必須在變量表格爲「光標」改變「someCursor」的類型,或者沒有去上班。

+0

+1:出色的響應 – kurosch 2009-12-21 22:27:23

0

SQL Developer與DBMS_OUTPUT工作得很好..它有一個單獨的標籤旁邊的腳本輸出,結果等只需點擊「啓用輸出」按鈕,並在您的代碼中使用DBMS_Output.Put_Line(result);