【问题标题】:Cannot call function which is defined in a PL/SQL package无法调用在 PL/SQL 包中定义的函数
【发布时间】:2018-11-17 21:05:04
【问题描述】:

我有这个 PL/SQL 包:

create or replace package palindrome as
    function check_palindrome(num int) return int;
end palindrome;


create or replace package body palindrome as
    function check_palindrome(num int) return int as 
        ans int;
        z int;
        r int;
        rev int;
    begin
        z := num;

        while z > 0 loop
            r := mod(z,10);
            rev := rev*10+r;
            z := floor(z/10);
        end loop;

        if rev=num then
            dbms_output.put_line('the no '||num ||' is a palindrome ');
        else
             dbms_output.put_line('the no '||num ||' is not a palindrome ');
        end if;     
     end check_palindrome;
end palindrome;

我创建了上面的包,它有一个函数check_palindrome(),但是当我尝试使用

调用该函数时
begin
    palindrome.check_palindrome(343);
end;

我收到此错误

Error report -
ORA-06550: line 2, column 5:
PLS-00221: 'CHECK_PALINDROME' is not a procedure or is undefined
ORA-06550: line 2, column 5:
PL/SQL: Statement ignored

为什么会出现此错误?包体编译成功,但调用函数时出现此错误。

【问题讨论】:

    标签: oracle plsql oracle11g


    【解决方案1】:

    您已经声明了一个 FUNCTION,它返回一个值,但您像调用 PROCEDURE 一样调用它,所以您需要:

    DECLARE
      the_val INT;
    BEGIN
      the_val := PALINDROME.check_palindrome(343);
    END;
    /
    

    【讨论】:

      【解决方案2】:

      您的程序实际上并没有返回任何内容,并且似乎没有任何价值来纠正它,因为您只是在屏幕上显示结果。相反,你应该把它变成一个过程:

      create or replace package palindrome as
          procedure check_palindrome(num int) ;
      end palindrome;
      
      
      create or replace package body palindrome as
          procedure check_palindrome(num int)  as 
              ans int;
              z int;
              r int;
              rev int;
          begin
              z := num;
      
              while z > 0 loop
                  r := mod(z,10);
                  rev := rev*10+r;
                  z := floor(z/10);
              end loop;
      
              if rev=num then
                  dbms_output.put_line('the no '||num ||' is a palindrome ');
              else
                   dbms_output.put_line('the no '||num ||' is not a palindrome ');
              end if;     
           end check_palindrome;
      end palindrome;
      

      那么就可以调用成功了:

      begin
          palindrome.check_palindrome(343);
      end;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-16
        • 1970-01-01
        • 1970-01-01
        • 2020-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多