强制值为空时无法引发VALUE_ERROR

时间:2019-03-30 00:34:55

标签: oracle stored-procedures plsql

employeeid,employeename和jobid是我表中的必填NOT NULL字段。如果其中任何一个为null,尽管我没有得到预期的结果,但我想引发VALUE_ERROR异常。

CREATE OR REPLACE PROCEDURE insert_employee(
    p_employeeid IN employees.employeeid%TYPE,
    p_employeename IN employees.employeename%TYPE,
    p_phone IN employees.phone%TYPE,
    p_jobid IN employees.jobid%TYPE,
    p_salary IN employees.salary%TYPE,
    p_managerid IN employees.managerid%TYPE,
    p_departmentid IN employees.departmentid%TYPE

) 

IS

BEGIN
    IF p_employeeid = NULL THEN
        RAISE VALUE_ERROR;

    ELSE IF p_employeename = NULL THEN
        RAISE VALUE_ERROR;

    ELSE IF p_jobid = NULL THEN
        RAISE VALUE_ERROR;

    ELSE
    INSERT INTO employees (employeeid, employeename, phone, jobid, salary, managerid, departmentid)
    VALUES(p_employeeid, p_employeename, p_phone, p_jobid, p_salary, p_managerid, p_departmentid);

    END IF;

EXCEPTION
    WHEN VALUE_ERROR THEN
    RAISE_APPLICATION_ERROR (06502, 'VALUE_ERROR');

END;

我收到此错误: 错误:PROCEDURE INSERT_EMPLOYEE行/颜色:30/1 PLS-00103:在预期以下情况之一时遇到符号“ EXCEPTION” :(如果case循环mod null编译指示提高返回选择更新,则使用go声明结束出口<<继续关闭当前删除取回锁定插入打开回滚保存点集sql执行commit forall合并管道清除json_exists json_value json_query json_object json_array Line / Col:34/4 PLS-00103:遇到符号“文件结尾”时,以下内容:end not pragma最终可实例化的持久性顺序重写静态成员构造函数映射

1 个答案:

答案 0 :(得分:0)

很明显,这是错误的,原因有几个。

  • 将值比较为NULL应该使用IS NULLIS NOT NULL,而不是使用等号=
  • RAISE_APPLICATION_ERROR的第一个参数应该为-20001-20999之间的数字(尽管正如您所说的那样,VALUE_ERROR是预定义的异常,您不必重新引发它)

我把它缩短了(不会像徒劳地打字那样):

SQL> CREATE OR REPLACE PROCEDURE insert_employee(
  2    p_employeeid     IN               NUMBER,
  3    p_employeename   IN               NUMBER,
  4    p_jobid          IN               NUMBER
  5  )IS
  6  BEGIN
  7    IF p_employeeid IS NULL THEN
  8      RAISE value_error;
  9    ELSIF p_employeename IS NULL THEN
 10      RAISE value_error;
 11    ELSIF p_jobid IS NULL THEN
 12      RAISE value_error;
 13    ELSE
 14      NULL;
 15    END IF;
 16  EXCEPTION
 17    WHEN value_error THEN
 18      raise_application_error(-20001,'This is a VALUE_ERROR');
 19  END;
 20  /

Procedure created.

SQL>
SQL> EXEC insert_employee(1,2,NULL);
BEGIN insert_employee(1,2,NULL); END;

*
ERROR at line 1:
ORA-20001: This is a VALUE_ERROR
ORA-06512: at "SCOTT.INSERT_EMPLOYEE", line 18
ORA-06512: at line 1


SQL> EXEC insert_employee(1,2,3);

PL/SQL procedure successfully completed.

SQL>