简体   繁体   English

更新前我的SQL触发器

[英]My SQL Trigger Before Update

I apologise if this question has been asked a million times before! 抱歉,如果这个问题已经被问了一百万遍了!

So I am trying to create a trigger on a table when an employee is assigned a task the pick_date is updated to show this. 因此,当员工分配了任务时,我正在尝试在表上创建触发器,pick_date更新以显示该任务。 It only should update the date field if its null. 如果日期字段为null,则仅应更新日期字段。 The order number, employee and order_pick_date are all on the same table. 订单号,雇员和order_pick_date都在同一张表上。

I have the following trigger but I can't seem to get it to run. 我有以下触发器,但似乎无法运行它。

CREATE TRIGGER pickDate BEFORE UPDATE
ON order_master
FOR EACH ROW BEGIN
UPDATE order_master SET NEW.order_pick_date = IFNULL(SYSDATE(),) WHERE  order_master.order_no = old.order_no
END;

A before update trigger is fine. before update触发器很好。 You just do not want to do an update . 您只是不想进行update Simply set the value on the new record: 只需在new记录上设置值:

CREATE TRIGGER pickDate BEFORE UPDATE
ON order_master
FOR EACH ROW BEGIN
    SET NEW.order_pick_date = COALESCE(old.order_pick_date, now()) ;
END;

There are some other issues (such as getdate() being SQL Server syntax), but I think this is what you intend. 还有其他一些问题(例如getdate()是SQL Server语法),但是我认为这是您想要的。

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

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