1442 - 无法更新存储函数/触发器中的表'ref_campaigns',因为它已被调用此存储函数/触发器的语句使用。
delimiter //
CREATE TRIGGER `campaigns_before_update` BEFORE UPDATE ON `ref_campaigns`
FOR EACH ROW BEGIN
# Required variable declaration
DECLARE lu datetime DEFAULT null;
DECLARE ra INT DEFAULT 0;
DECLARE raf INT DEFAULT 0;
DECLARE duration_calc INT DEFAULT 0;
DECLARE amount_calc FLOAT(8,2) DEFAULT 0;
#check campaign status
if NEW.status!=OLD.status and (NEW.status=1 || OLD.status=1) then
if NEW.status=1 then
set NEW.last_uptime_at = current_timestamp;
else
select last_uptime_at into lu from ref_campaigns where id = NEW.id;
if lu is not null then
select recurring_amount,recurring_amount_frequency into ra,raf from ref_campaign_pricing_plans where campaign_id = NEW.id;
select TIME_TO_SEC(TIMEDIFF(current_timestamp,lu)) into duration_calc;
if ra!=0 and raf!=0 then
set amount_calc = (ra*duration_calc)/(raf*24*60*60);
end if;
update ref_campaign_balances set amount_left = amount_left - amount_calc, duration_left = duration_left - duration_calc where campaign_id = NEW.id;
set NEW.last_uptime_at = current_timestamp;
end if;
end if;
end if;
END //
delimiter ;