简体   繁体   English

更改错误日期的 SQL 触发器

[英]SQL trigger that changes the wrong date

how can I make an SQL trigger in Oracle, which will check day and month of date inserted and if it's not Jan 1st, the trigger updates it?如何在 Oracle 中创建 SQL 触发器,它会检查插入日期的日期和月份,如果不是 1 月 1 日,触发器会更新它?

Example:例子:

CREATE TABLE Contract (
Ends_on DATE NOT NULL 
);

INSERT INTO Contract (Ends_on)
VALUES (TO_DATE('2017-1-30','yyyy-mm-dd'));

in this case, the date should automatically change to '2017-1-1'在这种情况下,日期应自动更改为“2017-1-1”

Thanks in advance提前致谢

The trigger would look like this, making use of the TRUNC function with second argument 'YEAR' :触发器看起来像这样,使用带有第二个参数'YEAR'TRUNC函数:

CREATE OR REPLACE TRIGGER trg_ins_contract
    BEFORE INSERT ON Contract 
    FOR EACH ROW
BEGIN
    :new.Ends_on := TRUNC(:new.Ends_on, 'YEAR');
END;
/

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

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