PL/SQL error: PLS-00103: Encountered the symbol "EXCEPTION"

PL/SQL error: PLS-00103: Encountered the symbol "EXCEPTION"

我正在制作一个 PL/SQL 错误处理模板。我收到此错误:'PLS-00103: Encountered the symbol "EXCEPTION"' 并且我不知道出了什么问题。我们正在使用 Oracle 数据库。

DECLARE
err_num VARCHAR(100); -- We're not gonna do math on this so I made it a string.
err_msg VARCHAR2(100);
program  VARCHAR2(100);
statement VARCHAR2(100);
BEGIN
program := 'assign the program here';
statment := 'assign the statement here';
END;


EXCEPTION


WHEN ACCESS_INTO_NULL
THEN
err_num := SQLCODE;
err_msg := SQLERRM(SQLCODE);
comment := "assign a comment here";
INSERT INTO ERRORS VALUES (err_num, err_msg, program, statement, comment, CURRENT_TIMESTAMP);

WHEN CASE_NOT_FOUND
THEN
err_num := SQLCODE;
err_msg := SQLERRM(SQLCODE);
comment := "assign a comment here";
INSERT INTO ERRORS VALUES (err_num, err_msg, program, statement, comment, CURRENT_TIMESTAMP);

...

WHEN OTHERS
THEN
err_num := -9999;
err_msg := 'undefined error occurred';
comment := "assign a comment here";
INSERT INTO ERRORS VALUES (err_num, err_msg, program, statement, comment, CURRENT_TIMESTAMP);


END;

错误 table 是:

CREATE TABLE ERRORS 
(
  ERR_NUM VARCHAR2(100 BYTE) NOT NULL 
, ERR_MSG VARCHAR2(100 BYTE) NOT NULL 
, PROGRAM VARCHAR2(100 BYTE) 
, STATEMENT VARCHAR2(100 BYTE) 
, USER_COMMENT VARCHAR2(100 BYTE) 
, TIMESTAMP DATE NOT NULL 
) 
LOGGING 
TABLESPACE DATAL03 
PCTFREE 10 
INITRANS 1 
STORAGE 
( 
  INITIAL 65536 
  MINEXTENTS 1 
  MAXEXTENTS UNLIMITED 
  BUFFER_POOL DEFAULT 
) 
NOCOMPRESS 
NOPARALLEL;

正如其他人在评论中所建议的那样,您需要做的就是删除第一个 END,如下所示:

DECLARE
err_num VARCHAR(100); -- We're not gonna do math on this so I made it a string.
err_msg VARCHAR2(100);
program  VARCHAR2(100);
statement VARCHAR2(100);
BEGIN
program := 'assign the program here';
statment := 'assign the statement here';



EXCEPTION


WHEN ACCESS_INTO_NULL
THEN
err_num := SQLCODE;
err_msg := SQLERRM(SQLCODE);
comment := "assign a comment here";
INSERT INTO ERRORS VALUES (err_num, err_msg, program, statement, comment,  CURRENT_TIMESTAMP);

WHEN CASE_NOT_FOUND
THEN
err_num := SQLCODE;
err_msg := SQLERRM(SQLCODE);
comment := "assign a comment here";
INSERT INTO ERRORS VALUES (err_num, err_msg, program, statement, comment, CURRENT_TIMESTAMP);

...

WHEN OTHERS
THEN
err_num := -9999;
err_msg := 'undefined error occurred';
comment := "assign a comment here";
INSERT INTO ERRORS VALUES (err_num, err_msg, program, statement, comment, CURRENT_TIMESTAMP);


END;

异常部分应该是 PL/SQL 块的一部分,而不是单独的部分。也就是说,它应该包含在 PL/SQL 块

BEGIN & END 语句中

Reference