How can I update table's column in a trigger after update on the same table?
Here's the trigger:
CREATE TRIGGER upd_total_votes AFTER UPDATE ON products_score
FOR EACH ROW
UPDATE
products_score
SET
products_score.votes_total =
(SELECT
(votes_1 + votes_2 + votes_3 + votes_4 + votes_5)
FROM
products_score
WHERE
id = new.id)
Now when I update the table like
UPDATE products_score SET votes_1 = 5 WHERE id = 0
this doesn't work, as I get the following:
#1442 - Can't update table 'products_score' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
So how on earth I can get this to work?
If you change your trigger to BEFORE instead of AFTER you could do it like this:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score
FOR EACH ROW
BEGIN
SET new.votes_total = new.votes_1 + new.votes_2 + new.votes_3 + new.votes_4 + new.votes_5
END
;
You can't have this the way you have setup because a trigger can't query other rows of the same table that it is defined on.
Istead you can use a Before Update Trigger toachieve what you want:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score FOR EACH ROW
BEGIN
SET NEW.votes_total = NEW.votes_1 + NEW.votes_2 + NEW.votes_3 + NEW.votes_4 + NEW.votes_5;
END;
Or use a stored procedure to update the table.
Related
So I have this trigger:
delimiter $$
CREATE TRIGGER check_liters
AFTER INSERT ON reports_data_sheet_area_brut
FOR EACH ROW BEGIN
IF (NEW.fuel > 10) THEN
UPDATE fuel_check
SET fuel_value = NEW.fuel
Where id = NEW.vehicule_id;
ELSEIF (NEW.fuel > 20) THEN
UPDATE fuel_check
SET fuel_value2 = NEW.fuel
Where id = NEW.vehicule_id;
END IF;
END$$
It can be added properly on triggers but it doesnt add data in fuel_check table when fuel>10 or 20.
This is reports_data_sheet_area_brut :
http://i.prntscr.com/a1f7af8c28c447e98eb0cf111e544596.png
This is fuel_check table(for the trigger):
http://i.prntscr.com/75f1467563d14febbdfd8b749a11013d.png
When I will try to add a value:
INSERT INTO reports_data_sheet_area_brut (vehicule_Id,area_Id,area,status_Id,status,start_time,end_time,duration,start_fuel_level_liters,end_fuel_level_liters,fuel,distance,consum_h )
VALUES
(43,432,"ceva",2,"Motor Oprit","2016-06-29 09:07:04","2016-06-30 10:07:06",30937,674,674,15,0,0);
After this SQL it only add column in reports_data_sheet_area_brut but not in fuel_check table from trigger.
How can I update table's column in a trigger after update on the same table?
Here's the trigger:
CREATE TRIGGER upd_total_votes AFTER UPDATE ON products_score
FOR EACH ROW
UPDATE
products_score
SET
products_score.votes_total =
(SELECT
(votes_1 + votes_2 + votes_3 + votes_4 + votes_5)
FROM
products_score
WHERE
id = new.id)
Now when I update the table like
UPDATE products_score SET votes_1 = 5 WHERE id = 0
this doesn't work, as I get the following:
#1442 - Can't update table 'products_score' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
So how on earth I can get this to work?
If you change your trigger to BEFORE instead of AFTER you could do it like this:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score
FOR EACH ROW
BEGIN
SET new.votes_total = new.votes_1 + new.votes_2 + new.votes_3 + new.votes_4 + new.votes_5
END
;
You can't have this the way you have setup because a trigger can't query other rows of the same table that it is defined on.
Istead you can use a Before Update Trigger toachieve what you want:
CREATE TRIGGER upd_total_votes BEFORE UPDATE ON products_score FOR EACH ROW
BEGIN
SET NEW.votes_total = NEW.votes_1 + NEW.votes_2 + NEW.votes_3 + NEW.votes_4 + NEW.votes_5;
END;
Or use a stored procedure to update the table.
I have the following trigger:
CREATE DEFINER = CURRENT_USER
TRIGGER `radia`.`orderstable_AFTER_UPDATE`
UPDATE ON `orderstable`
FOR EACH ROW begin
SET #VATCharged = Amount * (VAT*0.01);
But the VATCharged column does not update after there is a change to the amount or VAT.
The table has the following columns; Order Number(INT), AccNo(VARCHAR), Invoice Number(VARCHAR), Description(VARCHAR), Amount(INT), VAT(INT) and VATCharged(INT).
What is the solution to this problem?
You need to specify old or new against the column to get either old value before update or new value after update
set #VATCharged := new.Amount * (new.VAT*0.01);
EDIT :
If you want to update the VATCharged column in the table, then the trigger needs to be before update something as
delimiter //
create trigger `radia`.`orderstable_AFTER_UPDATE`
before update on `orderstable`
for each row
begin
set new.VATCharged = new.Amount * (new.VAT*0.01);
end ; //
delimiter ;
I'm trying to get this trigger to work:
CREATE TRIGGER Transaction_insert BEFORE INSERT ON Transaction
FOR EACH ROW WHERE Number = NEW.AccountNumber
IF Account.CreditBalance + NEW.Amount < Account.CreditLimit THEN
UPDATE Account SET CreditBalance = CreditBalance + NEW.Amount where Number = NEW.AccountNumber;
ELSE
SET NEW.Valid = 0
END IF;
This is the error I get from myPHPAdmin.
Your IF needs to be a full SELECT to reference another table (Account)
IF EXISTS (SELECT * FROM `Account` A
WHERE A.CreditBalance + NEW.Amount < A.CreditLimit AND
A.Number = NEW.AccountNumber) THEN
UPDATE ...
Edit: this was on your 2nd duplicate answer
In this case, remove the WHERE after FOR EACH ROW
Updated Answer
This is what I think you want, assuming that Account to Transaction is a 1:N relationship keyed on Number/AccountNumber:
DELIMITER //
-- Assumptions:
-- 1. Transaction.AccountNumber is F.K. REFERENCES Account(Number)
-- 2. Account.Number is UNIQUE
--
CREATE TRIGGER trg_bi_transaction BEFORE INSERT ON Transaction
FOR EACH ROW
BEGIN
-- Adjust account balance (if permitted)
--
UPDATE Account
SET CreditBalance = CreditBalance + NEW.Amount
WHERE Number = NEW.AccountNumber
AND
(CreditBalance + NEW.Amount) < CreditLimit;
-- Was the adjustment valid/permitted?
--
SET NEW.Valid = (ROW_COUNT() = 1);
END //
DELIMITER ;
That trigger will attempt to UPDATE the proper Account for any given Transaction if the CreditLimit permits. The Valid field will be set to 1 if the UPDATE succeeded, and 0 if it did not.
Original Answer
MySQL triggers do not support trigger-level WHERE clauses. Move the Number/NEW.AccountNumber check inside the trigger body.
hi
i have created a trigger which i want to update a table field when insertion in another field occurs.but i don,t know how should it know that its updating the same user row who is inserting here is some code and database senerio
--> admin_reg_abr
'id course_limit username
10 0 ahmad '
note: id is primry key
--> abroad_course
'id uni_id username
1 10 ahmad'
note: uni_id is foriegn key or same as id in admin_reg_abr.
i write the following code for trigger:
DROP TRIGGER IF EXISTS
ahmad.AI_course_each//
CREATE TRIGGER ahmad.AI_course_each
AFTER INSERT ON ahmad.abroad_course
FOR EACH ROW BEGIN
UPDATE admin_reg_abr SET
admin_reg_abr.course_limit =
admin_reg_abr.course_limit + 1 WHERE
admin_reg_abr.id = new.id; END //
i want to update the same id how insert in the table but its not working.
Didn't You mean:
DROP TRIGGER IF EXISTS ahmad.AI_course_each//
CREATE TRIGGER ahmad.AI_course_each
AFTER INSERT ON ahmad.abroad_course FOR EACH ROW
BEGIN
UPDATE admin_reg_abr SET
admin_reg_abr.course_limit = admin_reg_abr.course_limit + 1
WHERE admin_reg_abr.id = new.uni_id;
END //
Note that in Your code NEW.id points nowhere in admin_reg_abr table
One more thing. You are talking about firing trigger on UPDATE, so, You would probably need another trigger:
DROP TRIGGER IF EXISTS ahmad.AI_course_each//
CREATE TRIGGER ahmad.AI_course_each
AFTER UPDATE ON ahmad.abroad_course FOR EACH ROW
BEGIN
UPDATE admin_reg_abr SET
admin_reg_abr.course_limit = admin_reg_abr.course_limit + 1
WHERE admin_reg_abr.id = new.uni_id;
END //