创建过程oracle sql developer时出现异常错误

Posted

技术标签:

【中文标题】创建过程oracle sql developer时出现异常错误【英文标题】:Exception error when creating procedure oracle sql developer 【发布时间】:2020-08-13 00:12:49 【问题描述】:

过去 2 小时我一直在绞尽脑汁,找不到解决此错误的方法。我正在创建一个简单的程序来寻找员工。 PL/SQL 不断给我错误。问题是什么 ?我在这里做错了什么?

这是我的程序:

create or replace PROCEDURE find_employee (employeeNo IN number) as
    INVALID_ID exception;
    TOO_MANY_ROWS exception;
    res number;
BEGIN
    dbms_output.enable;
    Select count(*) into res from employee where ID=employeeNo;
    if (res>1)then      -- Checking that the total count of the employee is 1 or not
        raise TOO_MANY_ROWS;                                            -- if greater then 1 then it raise TOO_MANY_ROWS error
    ELSE IF (NOT EXISTS (Select ID from employee where ID=employeeNo))  -- Checking that the employeeNo user passes exist or not
    then
        raise INVALID_ID;                                               -- if employeeNo doesnot exit then display invalid id message
    ELSE
        Select* from Employee where ID=employeeNo;                      -- else return employee info whose id==employeeNo
    END IF;
EXCEPTION
    when TOO_MANY_ROWS then
        DBMS_OUTPUT.PUT_LINE ('Too many Rows with same employee id');
    when INVALID_ID then
        DBMS_OUTPUT.PUT_LINE ('Invalid employee id');
END find_employee;

错误是这样的:

Error(15,1): PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:     ( begin case declare end exit for goto if loop mod null    pragma raise return select update while with <an identifier>    <a double-quoted delimited-identifier> <a bind variable> <<    continue close current delete fetch lock insert open rollback    savepoint set sql execute commit forall merge pipe purge 

Error(20,18): PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:     end not pragma final instantiable order overriding static    member constructor map 

请大神帮帮我:'(

【问题讨论】:

【参考方案1】:

您缺少END IF第 16 行)。如果您编写格式化代码(嵌套的IF 应该缩进),则更容易发现它。

SQL> create or replace PROCEDURE find_employee (employeeNo IN number) as
  2      INVALID_ID exception;
  3      TOO_MANY_ROWS exception;
  4      res number;
  5  BEGIN
  6      dbms_output.enable;
  7      Select count(*) into res from employee where ID=employeeNo;
  8      if (res>1)then      -- Checking that the total count of the employee is 1 or not
  9          raise TOO_MANY_ROWS;                                            -- if greater then 1 then it raise TOO_MANY_ROWS error
 10      ELSE IF (NOT EXISTS (Select ID from employee where ID=employeeNo))  -- Checking that the employeeNo user passes exist or not
 11      then
 12          raise INVALID_ID;                                               -- if employeeNo doesnot exit then display invalid id message
 13      ELSE
 14          Select* from Employee where ID=employeeNo;                      -- else return employee info whose id==employeeNo
 15      END IF;
 16      END IF;        --> this is missing
 17  EXCEPTION
 18      when TOO_MANY_ROWS then
 19          DBMS_OUTPUT.PUT_LINE ('Too many Rows with same employee id');
 20      when INVALID_ID then
 21          DBMS_OUTPUT.PUT_LINE ('Invalid employee id');
 22  END find_employee;

正如@Dornaut 评论的那样,该代码可能不是 最好的 可以生成的。这是另一种选择;看看有没有帮助。

CREATE OR REPLACE PROCEDURE find_employee (employeeNo IN NUMBER)
AS
   res    NUMBER;
   e_row  employee%ROWTYPE;
BEGIN
   SELECT *
     INTO e_row
     FROM employee
    WHERE id = employeeNo;
EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
      DBMS_OUTPUT.put_line ('Invalid employee ID');
   WHEN TOO_MANY_ROWS
   THEN
      DBMS_OUTPUT.put_line ('Too many rows with same employee ID');
END find_employee;

所以:如果SELECT 返回NO_DATA_FOUNDTOO_MANY_ROWS,它将被处理。否则,它会将整行提取到一个变量中。

【讨论】:

仅在第 10 行使用 'ELSIF' 不是更简洁易读吗?它还会在当前状态下编译吗?看起来没有,因为 SQL 之外的“不存在”和没有“into”的 select 语句(顺便说一句,这个 select 实际上没有做任何事情......)。 你说得对,@Dornaut。我添加了更多信息。感谢您的评论。

以上是关于创建过程oracle sql developer时出现异常错误的主要内容,如果未能解决你的问题,请参考以下文章

ORA-02292: 违反完整性约束 - 在 ORACLE SQL Developer 中创建过程时发现子记录?

oracle sql developer的安装过程和使用说明

这个 Oracle SQL Developer 图标在我的存储过程中是啥意思?

oracle sql developer oracle-00904 存储过程执行错误

Oracle SQL Developer调试Oracle存储过程

如何在 SQL Developer 中检查语法 Oracle 存储过程?