我有以下触发器:
create or replace trigger CHILD_DOB_TRIGGER
before insert on CHILDREN
for each row
begin
if :NEW.CHILD_DOB > sysdate then
raise_application_error(-20001, 'CHILD_DOB must be greater than current date');
end if;
end;
/
但如果我尝试插入:
INSERT INTO CHILDREN
VALUES ((TO_DATE('2020/05/03', 'yyyy/mm/dd')));
我收到以下错误:
SQL Error: ORA-20001: CHILD_DOB must be greater than current date
ORA-06512: at "USER.CHILD_DOB_TRIGGER", line 3
ORA-04088: error during execution of trigger 'USER.CHILD_DOB_TRIGGER'
答案 0 :(得分:1)
您的触发器没有任何问题。由于调用了raise_application_error,它只是抛出异常ORA-20001。
在这种情况下,Oracle会在堆栈中引发更多异常:您收到的信息是INSERT
声明失败
ORA-04088: error during execution of trigger 'USER.CHILD_DOB_TRIGGER'
下一个消息是你的代码异常出现的地方
ORA-06512: at "USER.CHILD_DOB_TRIGGER", line 3
,最后一条消息显示主要问题
ORA-20001: CHILD_DOB must be greater than current date
答案 1 :(得分:0)
if:NEW.CHILD_DOB> sysdate然后 raise_application_error(-20001,'CHILD_DOB必须大于当前日期');
如果新插入日期 更高而 SYSDATE ,则会引发用户定义的例外。
TO_DATE('2020/05/03','yyyy / mm / dd')
上述日期更大比SYSDATE更强大,因此您的触发器完全符合您的要求。
例如,
SQL> CREATE OR REPLACE TRIGGER CHILD_DOB_TRIGGER
2 before insert on emp
3 for each row
4 BEGIN
5 if :NEW.hiredate > sysdate then
6 raise_application_error(-20001, 'CHILD_DOB must be greater than current date');
7 end if;
8 END;
9 /
Trigger created.
SQL>
SQL> sho err
No errors.
SQL>
SQL> INSERT INTO emp(hiredate)
2 VALUES ((TO_DATE('2020/05/03', 'yyyy/mm/dd')));
INSERT INTO emp(hiredate)
*
ERROR at line 1:
ORA-20001: CHILD_DOB must be greater than current date
ORA-06512: at "LALIT.CHILD_DOB_TRIGGER", line 3
ORA-04088: error during execution of trigger 'LALIT.CHILD_DOB_TRIGGER'
SQL>