【问题标题】:Package in Oracle (facing error while calling it)Oracle 中的包(调用时遇到错误)
【发布时间】:2014-10-06 20:18:52
【问题描述】:

我正在使用 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.

请帮忙。无法理解问题所在。

【问题讨论】:

  • 存储过程在 Oracle 中使用 execute 命令运行 - 至少在 SQL*Plus 和 SQL Developer 中是这样。
  • 它与执行命令一起工作。我也创建了其他包,但它们是由“调用”命令执行的。为什么会这样
  • 我谈到的另一个包只有过程,没有函数。是不是这个原因。

标签: oracle plsql


【解决方案1】:

CALL 是 SQL 语句,不是 PL/SQL 语句,也不是 SQLPLUS 命令。 CALL 可以直接在 SQLPLUS 中使用,但是,当你执行一个函数时,返回的结果应该存储在某个地方,因此需要 call 语句的 into 子句。并且括号,即使函数或存储过程没有参数,也是强制性的。

这是一个例子:

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.

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

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 

【讨论】:

    【解决方案2】:

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

    BEGIN
    CALL the_pack.the_proc;
    END;
    /
    

    【讨论】:

    • 不,很遗憾这是call 语句的错误用法。
    猜你喜欢
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 2018-05-04
    相关资源
    最近更新 更多