MySQL 在新价目表激活后触发将产品价格状态更改为非活动
MySQL trigger to change products price status to inactive after a new price list become active
我需要创建更新前触发器,以便在新产品价格变为 'active'
后更改“旧”产品价格的状态
价目表table:
id product_id supplier list_price_uom start_date price_status
1 101761 ABC 16.26 2021-02-01 Active
2 101761 DEF 19.5 2021-07-01 Active
3 1086 ABC 32.52 2021-02-01 Active
4 1087 ABC 32.52 2021-02-01 Active
5 AWBWHL ABC 82.23 2021-02-01 Active
6 101761 ABC 19.26 2021-08-30 Scheduled
7 1086 ABC 37.52 2021-08-30 Scheduled
8 1087 ABC 34.52 2021-08-30 Scheduled
9 AWBWHL ABC 85.23 2021-08-30 Scheduled
当 start_date = curdate()
时,我有一个事件将状态更新为 'Active'
CREATE EVENT "change_status"
ON SCHEDULE EVERY 1 DAY STARTS
'2021-08-26 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO UPDATE
price_list_test SET status = 'Active' WHERE status = 'Scheduled'
AND start_date = curdate()
当事件更新相同的 SKU(来自同一供应商)时,我需要将所有 'active' 行更改为 'inactive'。
我尝试了不同的变体:
BEFORE UPDATE ON "price_list_test" FOR EACH ROW
BEGIN IF (new.product_id = old.product_id) and (new.supplier = old.supplier) and (old.start_date < CURDATE()) THEN SET old.price_status = 'Inactive';
end if ;
END
这个给我错误“触发器中不允许更新旧行:
删除“旧”给我错误“未知系统变量 'price_status'”
试过这个:
BEFORE UPDATE ON "price_list_test" FOR EACH ROW BEGIN
UPDATE price_list_test SET price_status = 'Inactive' where new.product_id = old.product_id and old.start_date < CURDATE()
;
这给了我这个错误
“错误代码:1442。无法更新存储的 function/trigger 中的 table 'price_list_test',因为它已被调用此存储的 function/trigger 的语句使用。”
这是有道理的,只是 运行 没有想法。
非常感谢任何帮助。
执行事件过程中的一切:
CREATE EVENT change_status
ON SCHEDULE EVERY 1 DAY
STARTS '2021-08-26 00:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO
UPDATE price_list_test t1
LEFT JOIN price_list_test t2 ON t1.SKU = t2.SKU /* replace with correct condition */
AND t2.status = 'Active'
SET t1.status = 'Active',
t2.status = 'Inactive'
WHERE t1.status = 'Scheduled'
AND start_date = CURRENT_DATE;
None 在这种情况下需要额外 operation/code。
我需要创建更新前触发器,以便在新产品价格变为 'active'
后更改“旧”产品价格的状态价目表table:
id product_id supplier list_price_uom start_date price_status
1 101761 ABC 16.26 2021-02-01 Active
2 101761 DEF 19.5 2021-07-01 Active
3 1086 ABC 32.52 2021-02-01 Active
4 1087 ABC 32.52 2021-02-01 Active
5 AWBWHL ABC 82.23 2021-02-01 Active
6 101761 ABC 19.26 2021-08-30 Scheduled
7 1086 ABC 37.52 2021-08-30 Scheduled
8 1087 ABC 34.52 2021-08-30 Scheduled
9 AWBWHL ABC 85.23 2021-08-30 Scheduled
当 start_date = curdate()
时,我有一个事件将状态更新为 'Active'CREATE EVENT "change_status"
ON SCHEDULE EVERY 1 DAY STARTS
'2021-08-26 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO UPDATE
price_list_test SET status = 'Active' WHERE status = 'Scheduled'
AND start_date = curdate()
当事件更新相同的 SKU(来自同一供应商)时,我需要将所有 'active' 行更改为 'inactive'。
我尝试了不同的变体:
BEFORE UPDATE ON "price_list_test" FOR EACH ROW
BEGIN IF (new.product_id = old.product_id) and (new.supplier = old.supplier) and (old.start_date < CURDATE()) THEN SET old.price_status = 'Inactive';
end if ;
END
这个给我错误“触发器中不允许更新旧行:
删除“旧”给我错误“未知系统变量 'price_status'”
试过这个:
BEFORE UPDATE ON "price_list_test" FOR EACH ROW BEGIN
UPDATE price_list_test SET price_status = 'Inactive' where new.product_id = old.product_id and old.start_date < CURDATE()
;
这给了我这个错误 “错误代码:1442。无法更新存储的 function/trigger 中的 table 'price_list_test',因为它已被调用此存储的 function/trigger 的语句使用。”
这是有道理的,只是 运行 没有想法。 非常感谢任何帮助。
执行事件过程中的一切:
CREATE EVENT change_status
ON SCHEDULE EVERY 1 DAY
STARTS '2021-08-26 00:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO
UPDATE price_list_test t1
LEFT JOIN price_list_test t2 ON t1.SKU = t2.SKU /* replace with correct condition */
AND t2.status = 'Active'
SET t1.status = 'Active',
t2.status = 'Inactive'
WHERE t1.status = 'Scheduled'
AND start_date = CURRENT_DATE;
None 在这种情况下需要额外 operation/code。