在Oracle中打包(在调用时遇到错误)

时间:2014-10-05 09:58:01

标签: oracle plsql

我正在使用Oracle 11G数据库。我创建了一个包,如下所述:

create or replace package forward_emp is    
  Function emp_sal_avg return number;   
  Procedure emp_new_sal;    
End forward_emp;    
/

package created.

然后我创建了包体:

create or replace package body forward_emp is
  Function emp_sal_avg return number is 
    avg_sal emp.salary%type;
  Begin
    Select avg (salary) into avg_sal 
     from emp;
    Return avg_sal;
  End  emp_sal_avg;
  Procedure emp_new_sal is 
  Begin
    Insert into Emp (salary) values (emp_sal_avg);
  End emp_new_sal;
End forward_emp;
/

Package body created.

现在,当我尝试调用包时,它显示错误。

Call forward_emp.emp_new_sal;
ORA- 06576: not a valid function or procedure.

请帮忙。无法理解这个问题。

2 个答案:

答案 0 :(得分:1)

CALL是SQL语句,不是PL / SQL语句,不是SQL PLUS命令。 CALL可以直接在SQL PLUS中使用,但是,当您执行函数时,返回的结果应存储在某处,因此需要into语句的call子句。括号,即使函数或存储过程没有参数,也是必需的。

以下是一个例子:

SQL> create or replace package pkg as
  2    function f1 return number;
  3    procedure p1;
  4  end;
  5  /

Package created.

SQL> create or replace package body pkg as
  2    function f1 return number is
  3    begin
  4     return 12345;
  5    end;
  6    procedure p1 is
  7    begin
  8      null; -- does nothing
  9    end;
 10  end;
 11  /

Package body created.

现在让我们执行使用PKG包定义的那些过程和函数:

-- variable, which is going to store result the function returns
SQL> variable f_res number;

-- executing the F1 function
SQL> call pkg.f1() into :f_res;

Call completed.

-- print the result
SQL> print f_res;

     F_RES                                                                      
----------                                                                      
     12345                                                                      

-- executing the P1 procedure
SQL> call pkg.p1();

Call completed.

如果我们在使用into语句执行存储过程或类型方法时只是省略括号或未指定CALL子句,则会发生这种情况:

SQL> call pkg.p1;
call pkg.p1
     *
ERROR at line 1:
ORA-06576: not a valid function or procedure name 

SQL> call pkg.f1 into :f_res;
call pkg.f1 into :f_res
     *
ERROR at line 1:
ORA-06576: not a valid function or procedure name 

SQL> call pkg.f1();
call pkg.f1()
     *
ERROR at line 1:
ORA-06576: not a valid function or procedure name 

答案 1 :(得分:0)

我认为您正在尝试将CALL用作SQL * Plus命令。正如AHWNN指出的那样,在sqlplus中,您将使用EXECUTE来运行该过程。但是,您可以使用匿名块在CALL中使用CALL进行测试:

BEGIN
CALL the_pack.the_proc;
END;
/