简体   繁体   English

ORACLE-检查日期触发器

[英]ORACLE - CHECK DATE TRIGGER

Why this code doesn't work in ORACLE ? 为什么此代码在ORACLE中不起作用?

CREATE TRIGGER chk_dates
BEFORE INSERT ON `job_history`
FOR EACH ROW
BEGIN
  IF (NEW.end_date < NEW.start_date) THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'end_date cannot be earlier than start_date';
  END IF;
END;

I have a log: ORA-00911:"invalid character" 我有一个日志:ORA-00911:“无效字符”

You are mixing MySQL syntax with Oracle. 您正在将MySQL语法与Oracle混合使用。 There is no SIGNAL SQLSTATE in Oracle. Oracle中没有SIGNAL SQLSTATE

In Oracle, use raise_application_error to raise error. 在Oracle中,使用raise_application_error引发错误。

BEGIN
  IF (:NEW.end_date < :NEW.start_date) THEN
    raise_application_error(-20001, 'end_date cannot be earlier than start_date');
  END IF;
END;

In this case, you don't even need a trigger as this can be easily achieved using a check constraint like this: 在这种情况下,您甚至不需要触发器,因为可以使用如下检查约束轻松实现:

alter table your_table
add constraint contraint_name check (end_date >= start_date);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM