仅当列值为今天的日期时才需要触发触发器

时间:2017-07-06 08:40:09

标签: mysql sql database date triggers

当column_value与

等特定场景匹配时,我想在数据库中应用触发器

goal表格中有像goal,status,start_Date,end_Date

这样的字段

现在我想改变目标的状态。当用户输入他/她的目标时,他/她填写end_Date。现在我想在end_Date与当前日期

匹配时更改目标的状态

实施例: -

+------+--------+--------------+-------------+
| GOAL | STATUS | START_DATE   | END_DATE    |
+------+--------+--------------+-------------+
|    1 | Active | 2017-07-03   | 2017-07-09  |
+------+--------+------------+---------------+

当END_DATE等于当前日期时,我想将状态'Active'更改为'Finished'

我希望我能理解我的问题。

提前致谢!

2 个答案:

答案 0 :(得分:0)

oracle表级触发器的主体看起来像这样......

BEGIN
  IF INSERTING and (:new.end_date = sysdate) THEN
    :NEW.goal_status := desired_value;
  ELSIF UPDATING AND (:new.end_date = sysdate) then
    :NEW.goal_status := desired_value;
  END IF;
End;

答案 1 :(得分:0)

这个的逻辑是

DROP TRIGGER IF EXISTS TB;
DELIMITER //
CREATE TRIGGER TB BEFORE INSERT ON T
FOR EACH ROW
BEGIN

 IF NEW.START_DT = DATE(NOW()) THEN
    SET NEW.STATUS = 'YES' ;
 end if;
END //
DELIMITER ;

  use sandbox;
DROP TABLE IF EXISTS T;
CREATE TABLE T(GOAL INT, STATUS VARCHAR(3), START_DT DATE,END_DATE DATE);
mysql> TRUNCATE TABLE T;INSERT INTO T VALUES(1,NULL,'2017-07-06','2017-07-06');SELECT * FROM T;
Query OK, 0 rows affected (0.73 sec)

Query OK, 1 row affected (0.06 sec)

+------+--------+------------+------------+
| GOAL | STATUS | START_DT   | END_DATE   |
+------+--------+------------+------------+
|    1 | YES    | 2017-07-06 | 2017-07-06 |
+------+--------+------------+------------+
1 row in set (0.00 sec)