强制值为空时无法引发 VALUE_ERROR
Posted
技术标签:
【中文标题】强制值为空时无法引发 VALUE_ERROR【英文标题】:Unable to raise VALUE_ERROR when mandatory values are null 【发布时间】:2019-03-30 00:34:54 【问题描述】:employeeid、employeename 和 jobid 是我的表中的强制 NOT 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 Line/Col:30/1 PLS-00103:在预期以下情况之一时遇到符号“EXCEPTION”:(begin case declare end exit for goto if loop mod null pragma raise return select update while with
【问题讨论】:
【参考方案1】:这显然是错误的,有几个原因。
应该使用IS NULL
或IS NOT NULL
将值与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>
【讨论】:
以上是关于强制值为空时无法引发 VALUE_ERROR的主要内容,如果未能解决你的问题,请参考以下文章
数据绑定值为空时如何在 DataGridViewComboBoxColumn 中显示文本