函数/包问题PL / SQL

时间:2013-04-16 15:43:51

标签: sql oracle function plsql package

我正在尝试创建一个包含四个函数的包。每个函数将添加一组数字并从总数中减去一个。我在使语法正确方面遇到了很多麻烦。下面的函数自己工作,我尝试在最后调用第一个函数。

当我尝试创建包时,我得到一个错误,在第7行,“当遇到以下之一时遇到符号”END“:开始函数编译指示程序子类型类型当前游标删除存在于0.05秒之前”

在包体中,它表示“名称已被现有对象使用”。我不明白,因为它必须在包的规范中声明,并且如果错误是已经存在名为functionbyfour的包,则创建或替换应解决此问题。

最后,当我尝试在包中使用一个函数时,它会在遇到以下其中一个时说“遇到符号”BEGIN“:=。(@%;非空范围默认字符符号”;“取代“BEGIN”继续.ORA-06550:第5行,第43栏:PLS-00103:当期待下列之一时遇到符号“FROM”:。(*%& = - +;< /> ; at in is mod remaining not rem<>或!=或〜=> =< =<>和/或类似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;

/

3 个答案:

答案 0 :(得分:8)

您需要创建名为FunctionByFourCREATE 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)。你还需要一个END的包体(现在,你的代码在第四个函数的末尾结束)

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以使用函数的结果填充局部变量(您也可以只为函数调用的结果分配局部变量,而无需使用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之前。

答案 1 :(得分: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. 单词second是关键词,您不能将其用作参数名称

  4. 你需要一个

    END functionbyfour;
    

    在你的END functfour之后结束包体

  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. 想做什么奇怪的事? : - )

答案 2 :(得分:3)

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

CREATE FUNCTION只能用于创建独立的功能。你有什么应该是:

CREATE OR REPLACE PACKAGE functionbyfour AS

包由两部分组成,即规范和正文。规范是API的公共面,正文是实现。你所拥有的包(规格)是包体。所以改变第二块代码开始

CREATE OR REPLACE PACKAGE BODY functionbyfour AS

至少你的程序结构正确。

Oracle PL / SQL文档是在线的,全面的,免费的。我劝你读一读。 Find out more