4

我正在尝试创建一个具有四个功能的包。每个函数将添加一组数字并从总数中减去一个。我在语法正确时遇到了很多麻烦。下面的函数独立工作,我尝试在最后调用第一个函数。

当我尝试创建包时,在第 7 行出现错误,“在预期以下情况之一时遇到符号“END”:开始函数 pragma 过程子类型类型当前光标删除在 0.05 秒之前存在”

在包正文中它说“名称已被现有对象使用”。我不明白,因为无论如何它都必须在包的规范中声明,如果错误是已经有一个名为 functionbyfour 的包,那么 create 或 replace 应该可以解决这个问题。

最后,当我尝试使用包中的函数时,它显示“遇到以下符号之一时遇到符号“BEGIN”::=。(@%;不是空范围默认字符符号“;”被替换为“BEGIN”继续。ORA-06550:第 5 行,第 43 列:PLS-00103:在预期以下之一时遇到符号“FROM”:. ( * % & = - + ; < /> 在 in 是 mod 余数not rem <> or != or ~= >= <= <> and or like2 like4 likec between || multiset me”。

我正在使用 ORACLE EXPRESS 版本 11g,并且是 PL/SQL 的新手(4 周)。

非常感谢任何输入。

CREATE OR REPLACE FUNCTION functionbyfour AS

 FUNCTION functone( first number, second number) RETURN NUMBER ;
 FUNCTION functtwo( first number, second number, third number) RETURN NUMBER ;
 FUNCTION functthree(first number, second number, third number, fourth number) RETURN     NUMBER ;
 FUNCTION functfour( first number, second number, third number, fourth number,fifth   number) RETURN NUMBER ;

END  functionbyfour;
/

CREATE OR REPLACE PACKAGE functionbyfour AS

FUNCTION functone (first number, second number ) RETURN number AS total number;
 BEGIN
total:=first + second – 1;
RETURN total;
DBMS_OUTPUT.PUT_LINE(total);
END functone;


FUNCTION functtwo (first number, second number, third number ) RETURN number AS  total     number;
BEGIN
total:=first + second + third – 1;
RETURN total;
DBMS_OUTPUT.PUT_LINE(total);
END functtwo;

FUNCTION functthree (first number, second number,third number, fourth number )     RETURN     number AS total number;
BEGIN
total:=first + second + third + fourth – 1;
RETURN total;
DBMS_OUTPUT.PUT_LINE(total);
END functthree;


FUNCTION functfour (first number, second number, third number, fourth number, fifth     number ) RETURN number AS total number;
BEGIN
total:=first + second + third + fourth + fifth – 1;
RETURN total;
DBMS_OUTPUT.PUT_LINE(total);
END functfour;

/

BEGIN

SELECT functionbyfour.functone(1,2) FROM DUAL;

END;

/​</p>

4

3 回答 3

9

您需要创建一个名为FunctionByFour( CREATE OR REPLACE PACKAGE)的包

SQL> ed
Wrote file afiedt.buf

  1  CREATE OR REPLACE PACKAGE functionbyfour AS
  2   FUNCTION functone( first number, second number) RETURN NUMBER ;
  3   FUNCTION functtwo( first number, second number, third number) RETURN NUMBER ;
  4   FUNCTION functthree(first number, second number, third number, fourth number) RETURN     NUMBER ;
  5   FUNCTION functfour( first number, second number, third number, fourth number,fifth   number) RETURN NUMBER ;
  6* END  functionbyfour;
  7  /

Package created.

然后是相应的包体 ( CREATE OR REPLACE PACKAGE BODY)。您还需要一个ENDfor 包体(现在,您的代码在第四个函数的末尾结束)

SQL> ed
Wrote file afiedt.buf

  1  CREATE OR REPLACE PACKAGE BODY functionbyfour AS
  2    FUNCTION functone (first number, second number ) RETURN number AS total number;
  3    BEGIN
  4      total:=first + second - 1;
  5      RETURN total;
  6      DBMS_OUTPUT.PUT_LINE(total);
  7    END functone;
  8    FUNCTION functtwo (first number, second number, third number ) RETURN number AS  total     number;
  9    BEGIN
 10      total:=first + second + third - 1;
 11      RETURN total;
 12      DBMS_OUTPUT.PUT_LINE(total);
 13    END functtwo;
 14    FUNCTION functthree (first number, second number,third number, fourth number )     RETURN     number AS total number;
 15    BEGIN
 16      total:=first + second + third + fourth - 1;
 17      RETURN total;
 18      DBMS_OUTPUT.PUT_LINE(total);
 19    END functthree;
 20    FUNCTION functfour (first number, second number, third number, fourth number, fifth     number ) RETURN number AS total number;
 21    BEGIN
 22      total:=first + second + third + fourth + fifth - 1;
 23      RETURN total;
 24      DBMS_OUTPUT.PUT_LINE(total);
 25    END functfour;
 26* END functionbyfour;
SQL> /

Package body created.

完成后,您可以使用该功能

SQL> SELECT functionbyfour.functone(1,2) FROM DUAL;

FUNCTIONBYFOUR.FUNCTONE(1,2)
----------------------------
                           2

如果要将SELECT语句放在 PL/SQL 块中,则需要声明一个局部变量并SELECT INTO使用函数的结果填充局部变量(您也可以只为局部变量分配函数调用而不需要使用 a SELECT)。

SQL> ed
Wrote file afiedt.buf

  1  DECLARE
  2    l_result NUMBER;
  3  BEGIN
  4    -- First approach
  5    l_result := functionByFour.functOne(1,2);
  6    dbms_output.put_line( l_result );
  7    -- Second approach
  8    SELECT functionByFour.functOne(1,2)
  9      INTO l_result
 10      FROM dual;
 11    dbms_output.put_line( l_result );
 12* END;
 13  /
2
2

PL/SQL procedure successfully completed.

另外,请注意,DBMS_OUTPUT.PUT_LINE在您的RETURN陈述之后加上一个是没有意义的。永远无法达到该代码。如果要将结果打印到DBMS_OUTPUT缓冲区,则需要在RETURN.

于 2013-04-16T15:57:10.903 回答
4
  1. 线

    CREATE OR REPLACE FUNCTION functionbyfour AS
    

    应该

    CREATE OR REPLACE PACKAGE functionbyfour AS
    
  2. 线

    CREATE OR REPLACE PACKAGE functionbyfour AS
    

    应该

    CREATE OR REPLACE PACKAGE BODY functionbyfour AS
    
  3. 第二个字是关键字,不能用作参数名

  4. 你需要一个

    END functionbyfour;
    

    在你的 END 函数结束包体之后

  5. 您的 dbms_outputs 将永远不会在返回后执行

  6. 你可以在一个函数中完成所有这些

     FUNCTION functall(FIRST NUMBER
                         ,sec NUMBER DEFAULT 0
                         ,third NUMBER DEFAULT 0
                         ,fourth NUMBER DEFAULT 0
                         ,fifth NUMBER DEFAULT 0)
      RETURN NUMBER
    AS
      total NUMBER;
    BEGIN
      total := first + sec + third + fourth + fifth - 1;
    
      dbms_output.PUT_LINE(total);
    
      RETURN total;
    END functall;
    
  7. 想要做什么奇怪的事情?:-)

于 2013-04-16T16:10:40.130 回答
3

您对独立程序和软件包感到困惑。

CREATE FUNCTION 只能用于创建独立的函数。你所拥有的应该是:

CREATE OR REPLACE PACKAGE functionbyfour AS

一个包由两部分组成,一个规范和一个主体。规范是 API 的公开面,主体是实现。您作为包(规格)所拥有的是包体。所以改变第二段代码开始

CREATE OR REPLACE PACKAGE BODY functionbyfour AS

至少你的程序结构正确。

Oracle PL/SQL 文档是在线的、全面的和免费的。我敦促你阅读它。 了解更多

于 2013-04-16T15:51:51.060 回答