收到错误 -> 发现 'begin' 需要外部语言

Getting an error -> found 'begin' expecting external language

我正在包内创建过程,但在执行以下脚本时出现以下错误

CREATE PACKAGE doc_interface AS

code_value                     VARCHAR2(30 CHAR)            ;

 PROCEDURE list_doc (
   lg_code    IN     fpl_ref.lang%TYPE,
   pl_no   IN     prt_req.pl_no%TYPE,
   cl_no    IN     prt_req.cl_no%TYPE,
   ct_no           IN      prt_req.ct_no%TYPE,
   potab_doc_list       OUT  custDocTab)
AS
BEGIN
   potab_doc_list := NEW custDocTab();

   IF pl_no IS NOT NULL AND cl_no IS NULL
   THEN
      select *
        BULK COLLECT INTO potab_doc_list
        from cs_record where req_id = 19736543;                          
   
   END IF;
EXCEPTION
   WHEN OTHERS
   THEN
      z_error ('Error while fetching records =' || SQLERRM);
END list_doc;

END doc_interface;

错误:

[Error] Syntax check (12: 0): ERROR line 13, col 1, ending_line 13, ending_col 5, Found 'BEGIN', Expecting: EXTERNAL LANGUAGE

不理解它在 BEGIN 之后的期望。

您不要将实现代码放在 PACKAGE (SPEC) 中,而是在 PACKAGE BODY 中定义它。

此外,如果您将 CODE_VALUE 放在正文与规范中,您将无法在包外引用它。

更像是-

CREATE PACKAGE DOC_INTERFACE AS 
 code_value                     VARCHAR2(30 CHAR)            ;

 PROCEDURE list_doc (
   lg_code    IN     fpl_ref.lang%TYPE,
   pl_no   IN     prt_req.pl_no%TYPE,
   cl_no    IN     prt_req.cl_no%TYPE,
   ct_no           IN      prt_req.ct_no%TYPE,
   potab_doc_list       OUT  custDocTab);
END DOC_INTERFACE;
/


CREATE PACKAGE BODY doc_interface AS

 PROCEDURE list_doc (
   lg_code    IN     fpl_ref.lang%TYPE,
   pl_no   IN     prt_req.pl_no%TYPE,
   cl_no    IN     prt_req.cl_no%TYPE,
   ct_no           IN      prt_req.ct_no%TYPE,
   potab_doc_list       OUT  custDocTab)
AS
BEGIN
   potab_doc_list := NEW custDocTab();

   IF pl_no IS NOT NULL AND cl_no IS NULL
   THEN
      select *
        BULK COLLECT INTO potab_doc_list
        from cs_record where req_id = 19736543;                          
   
   END IF;
EXCEPTION
   WHEN OTHERS
   THEN
      z_error ('Error while fetching records =' || SQLERRM);
END list_doc;

END doc_interface;
/

您使用`CREATE PACKAGE 显示的代码正在创建包规范 - 但您已经包含了程序主体,它属于包主体。您需要分别创建规范和正文:

CREATE PACKAGE doc_interface AS

code_value                     VARCHAR2(30 CHAR)            ;

 PROCEDURE list_doc (
   lg_code    IN     fpl_ref.lang%TYPE,
   pl_no   IN     prt_req.pl_no%TYPE,
   cl_no    IN     prt_req.cl_no%TYPE,
   ct_no           IN      prt_req.ct_no%TYPE,
   potab_doc_list       OUT  custDocTab);

END doc_interface;
/

然后

CREATE PACKAGE BODY doc_interface AS

 PROCEDURE list_doc (
   lg_code    IN     fpl_ref.lang%TYPE,
   pl_no   IN     prt_req.pl_no%TYPE,
   cl_no    IN     prt_req.cl_no%TYPE,
   ct_no           IN      prt_req.ct_no%TYPE,
   potab_doc_list       OUT  custDocTab)
AS
BEGIN
   potab_doc_list := NEW custDocTab();

   IF pl_no IS NOT NULL AND cl_no IS NULL
   THEN
      select *
        BULK COLLECT INTO potab_doc_list
        from cs_record where req_id = 19736543;                          
   
   END IF;
EXCEPTION
   WHEN OTHERS
   THEN
      z_error ('Error while fetching records =' || SQLERRM);
END list_doc;

END doc_interface;
/

尚不清楚您是否希望 code_value 在包规范中声明 - 它将在 public 中 - 或在包主体中 - 它将是私有的。

Read more about package structure:

A package always has a specification, which declares the public items that can be referenced from outside the package.

If the public items include cursors or subprograms, then the package must also have a body. The body must define queries for public cursors and code for public subprograms. The body can also declare and define private items that cannot be referenced from outside the package, but are necessary for the internal workings of the package.

如果您使用 have/need 规范级变量,还请阅读 package state