Oracle
在 PL/SQL 中執行包內的函式時遇到錯誤
大家好,希望你們一切都好。我是甲骨文的新手,我正在閱讀“包”和“包體”的概念。我有一個功能,如下所示
CREATE OR REPLACE Function Show_description(i_Course_No Course.Course_No%Type) Return Varchar2 AS V_Description Varchar2(50); Begin Select description into v_Description From Course Where Course_No = I_Course_No; Return V_Description; Exception When No_Data_Found Then Return('The Course Is not in the DataBase'); When Others Then Return('Error in running Show_Description'); End;
我有一個只包含這個功能的包:
CREATE OR REPLACE PACKAGE MY_First_Package is function Show_description(i_Course_No Course.Course_No%Type) Return Varchar2; end ;
我有一個包裹體,你可以在這裡看到:
create or replace package body my_first_package AS Function Show_description(i_Course_No Course.Course_No%Type) Return Varchar2 AS V_Description Varchar2(50); Begin Select description into v_Description From Course Where Course_No = I_Course_No; Return V_Description; Exception When No_Data_Found Then Return('The Course Is not in the DataBase'); When Others Then Return('Error in running Show_Description'); End; End my_first_package;
一切似乎都很好,沒有任何錯誤。當我想將此包執行為:
exec my_first_package.Show_description(12);
我面臨一個錯誤,即:
ORA-00900 :Invalid sql statement
問題是什麼??提前致謝
嗯,是的——但不一定。正如您想使用的那樣
EXEC
(即 SQL*Plus),那麼看看您是如何做到的;首先,一個具有返回某個部門編號的部門名稱的函式的包。SQL> create or replace package my_first_package as 2 function show_Description(par_deptno in varchar2) return varchar2; 3 end; 4 / Package created. SQL> create or replace package body my_first_package as 2 function show_Description(par_deptno in varchar2) 3 return varchar2 4 is 5 retval dept.dname%type; 6 begin 7 select dname 8 into retval 9 from dept 10 where deptno = par_deptno; 11 return retval; 12 exception 13 when no_data_found then 14 return null; 15 end; 16 end; 17 / Package body created.
現在,如何使用
EXEC
:先聲明一個變數,然後…SQL> var l_ret varchar2(20); SQL> exec :l_ret := my_first_package.show_description(10); PL/SQL procedure successfully completed. SQL> print l_ret; L_RET ------------------------------------------------------------- ACCOUNTING SQL>
如果您想在 PL/SQL 中使用該函式,那麼您也可以這樣做 - 聲明一個變數並將函式值返回到其中,例如
declare l_ret varchar2(20); begin some code here l_ret := my_first_package.show_description(10); ... end;
或者,您可以在例如
WHERE
子句中“直接”使用它:begin select ... into ... from ... where description = my_first_package.show_description(10) and ... end;