-1

我试图在 plsql 中创建 2 个包体。这是我的代码:

SET SERVEROUTPUT ON

CREATE OR REPLACE PACKAGE p_locations
AS
  FUNCTION f_distance(Lat1 IN NUMBER, Lon1 IN NUMBER, Lat2 IN NUMBER, Lon2 IN NUMBER, Radius IN NUMBER DEFAULT 6387.7) return number;
END p_locations;

/

CREATE OR REPLACE PACKAGE BODY p_locations
AS
  FUNCTION f_distance (Lat1 IN NUMBER, Lon1 IN NUMBER, Lat2 IN NUMBER, Lon2 IN NUMBER, Radius IN NUMBER DEFAULT 6387.7)
  RETURN NUMBER 
  IS
   -- Convert degrees to radians
   DegToRad NUMBER := 57.29577951;

  BEGIN
    RETURN(NVL(Radius,0) * ACOS((sin(NVL(Lat1,0) / DegToRad) * SIN(NVL(Lat2,0) / DegToRad)) +
          (COS(NVL(Lat1,0) / DegToRad) * COS(NVL(Lat2,0) / DegToRad) *
           COS(NVL(Lon2,0) / DegToRad - NVL(Lon1,0)/ DegToRad))));
  END f_distance;
END p_locations;

/

CREATE OR REPLACE PACKAGE p_winkel
AS
  FUNCTION changeOpeningstijd("id" IN number) RETURN boolean;
END p_winkel;

/

CREATE OR REPLACE PACKAGE BODY p_winkel
AS
  FUNCTION changeOpeningstijd("id" IN number) 
  RETURN boolean
  IS
    dbms_output.put_line('dit is uitgevoerd');
    return true;
  END changeOpeningstijd;
END p_winkel;

当我运行它时,我遇到了 3 次 PLS-00103 错误。第一个在第 6,16 行,说遇到了符号“。” 当期待以下之一时:constant exception <an identifier> <a double-quoted delimited-identifier> table long double ref char time timestamp interval date binary national character nchar The symbol "<an identifier>" was substituted for "." to continue.

奇怪的是,当我注释掉第二个包体时,一切正常。虽然错误出现在第一个包定义的开头。

我在这里做错了什么愚蠢的事情,或者你不能在一个会话中创建两个包,或者这里发生了什么,因为我在这些错误中看不到任何逻辑。

4

1 回答 1

1

您只是缺少BEGIN关键字:

CREATE OR REPLACE PACKAGE BODY p_winkel
AS
  FUNCTION changeOpeningstijd("id" IN number) 
  RETURN boolean
  IS
  BEGIN    ---- this was missing
    dbms_output.put_line('dit is uitgevoerd');
    return true;
  END changeOpeningstijd;
END p_winkel;
/

PL/SQL 错误中的行号指的是由它引起的 PL/SQL 块(在本例中为包);它不是组合脚本中的行号,就像普通 SQL 错误一样。

当您运行此程序时,run script您会报告三个错误,而不仅仅是您提到的那个;另外两个都提到begin

Errors: check compiler log
6/16           PLS-00103: Encountered the symbol "." when expecting one of the following:

   constant exception <an identifier>
   <a double-quoted delimited-identifier> table long double ref
   char time timestamp interval date binary national character
   nchar
The symbol "<an identifier>" was substituted for "." to continue.

8/3            PLS-00103: Encountered the symbol "END" when expecting one of the following:

   begin function pragma procedure subtype type <an identifier>
   <a double-quoted delimited-identifier> current cursor delete
   exists prior
The symbol "begin was inserted before "END" to continue.

9/13           PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

   begin end function pragma procedure

正如 Ben 提到的,show errors在每个规范/正文定义之后添加一个以突出显示错误的位置是个好主意;但您也可以查询user_errors视图以查看与每个无效对象相关的错误。

于 2015-01-06T08:37:05.583 回答