Mysql TRIGGER ON UPDATE with Functions - mysql

im trying to make an trigger that updates a table when i modify another table...
Here is my code:
CREATE TRIGGER updpartido AFTER UPDATE ON partidos
ON EACH ROW
BEGIN
SET #vgls = SELECT vgoles(NEW.eqvis)
SET #lgls = SELECT vgoles(NEW.eqloc)
UPDATE equipos SET gf=#vgls WHERE id=NEW.eqvis
UPDATE equipos SET gf=#lgls WHERE id=NEW.eqloc
END
What it must do is, when i update an match, it must automaticly run this trigger and update the goals.
But it gives me an error.
What im doing wrong? Thanks and have a nice day...!

I notice that you don't have any statement terminators in your trigger, that will generate some complaints because, for example, SET #lgls = SELECT vgoles(NEW.eqloc) UPDATE equipos SET gf=#vgls WHERE id=NEW.eqvis doesn't make any sense. So you need some semicolons in the trigger but you also need to get those semicolons past the parser by temporarily changing the delimiter. You're also using ON EACH ROW when it should be FOR EACH ROW:
delimiter |
CREATE TRIGGER updpartido AFTER UPDATE ON partidos
FOR EACH ROW
BEGIN
SET #vgls = SELECT vgoles(NEW.eqvis);
SET #lgls = SELECT vgoles(NEW.eqloc);
UPDATE equipos SET gf=#vgls WHERE id=NEW.eqvis;
UPDATE equipos SET gf=#lgls WHERE id=NEW.eqloc;
END;
|
delimiter ;

Related

Set 2 colums after an update on a table

So I am trying to to put in capital letter 2 fields from my table after an insert. So I created a trigger but since I want to update 2 fields, I am not sure how to exactly do this. This is what I did but I don't think it's the right way to do this. I want to set these 2 fields in capital letter when there is an insert.
Thank you :)
CREATE or ALTER TRIGGER majVilleClient
on client
instead of insert
as
begin
update client
set nom_client =upper(nom_client)
where nom_client in (select nom_client from inserted)
set nom_ville = upper(nom_ville)
where nom_ville in (select nom_ville from inserted)
end```
i am infamiliar wth your rdms but sure it osn't MySQL, you should change that
But when your first update worked, yu can use the following.
CREATE or ALTER TRIGGER majVilleClient
on client
instead of insert
as
begin
update client
set nom_client =upper(nom_client)
,nom_ville = upper(nom_ville)
where nom_client in (select nom_client from inserted)
end
In MySQL, which you tagged your question with, you would use a BEFORE INSERT trigger, so you can change the values before they are written:
delimiter //
create trigger majvilleclient
before update on client
for each row
begin
set new.nom_client = upper(new.nom_client);
set new.nom_ville = upper(new.nom_ville);
end;
//
delimiter ;
Update that's what I did but I am not sure if this would works.
CREATE or ALTER TRIGGER majVilleClient
on client
after insert
as
begin
update client
set nom_client =upper(nom_client),
nom_ville = upper(nom_ville)
where nom_ville in (select nom_ville from inserted) and nom_client in (select nom_client from inserted)
end```

Question about Update on Trigger with condition

I can't find the correct syntax for the trigger I tried to code.
I want to update a timestamp every time there is an update on the table but only for specific rows. I want to update the "UPDATE_TIME" only for the last "N_TACHE"
I can't find how to use SET with a where clause.
CREATE TRIGGER "time_on_update"
BEFORE UPDATE ON "SAVE_UPDATE"
FOR EACH ROW
BEGIN
IF
(NEW.DATE_DE_RDV <> OLD.DATE_DE_RDV)
OR
(NEW.ADRESSE <> OLD.ADRESSE)
OR
(NEW.CODE_POSTAL <> OLD.CODE_POSTAL)
OR
(NEW.VILLE <> OLD.VILLE)
THEN
SET (NEW.UPDATE_TIME = current_timestamp WHERE N_TACHE= (SELECT MAX(N_TACHE));
END IF;
If you have any suggestion I take it.
Thanks in advance
EDIT :
This one is working but it update all the rows and I just want to update "update_time" for the last value of "N_TACHE" when "RDV","ADRESSE","CODE_POSTAL" or "VILLE" is updated
DELIMITER $$
CREATE TRIGGER `time_on_update`
BEFORE UPDATE ON `SAVE_UPDATE`
FOR EACH ROW
BEGIN
IF
(SELECT MAX(N_TACHE)
FROM SAVE_UPDATE
WHERE
((NEW.DATE_DE_RDV <> OLD.DATE_DE_RDV)
OR
(NEW.ADRESSE <> OLD.ADRESSE)
OR
(NEW.CODE_POSTAL <> OLD.CODE_POSTAL)
OR
(NEW.VILLE <> OLD.VILLE)))
THEN
SET NEW.UPDATE_TIME = current_timestamp;
END IF;
END
$$
DELIMITER ;

MYSql after update trigger not updating 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 ;

Mysql issue update command in before update trigger

Looking for assistance crafting a mysql trigger. My current code does not work as intended. What I would like to do is if field A in table A is modified, copy field A to field B in table A.
Current code looks like this:
BEGIN
IF new.set_id=301 THEN
UPDATE lighting_io_settings SET slider1_val=new.val WHERE set_id=402;
END IF;
END
Obviously it fails because an update is calling an update.
On before update you can check the old tuple value on variable "OLD" and check the new tuple values on "NEW" variable.
BEGIN
IF NEW.columnA != OLD.columnA THEN
//do whatever you want here like
NEW.columnB = NEW.columnA;
//can call update again, just don't change the columnA
update tableA set columnB = NEW.columnA where id = 402;
END IF;
END

MySQL nested Conditional in Update Trigger

I am trying to figure out a conditional trigger for an update to a MySQL table.
The issue is that the update trigger fires off even when the data being entered is the same as the data that was already there. So if I have a column with a row:value set up like this plus_votes:2 if I update with UPDATE votes SET plus_votes = 2; even though nothing has really changed the UPDATE trigger still gets fired off. So to prevent all of this I want to add a conditional clause to my trigger like this
BEGIN
IF NEW.vote_value <> OLD.vote_value THEN
UPDATE my_other_table
SET plus_votes =
CASE NEW.vote_value WHEN '1'
THEN plus_votes + 1
ELSE plus_votes
END,
minus_votes =
CASE NEW.vote_value WHEN '1'
THEN minus_votes
ELSE minus_votes +1
END
WHERE my_other_table.id=NEW.votes_join_id;
END
END
the second half of the issue is that I have CASE condition inside the IF statement.
Can someone help on this issiu and show how to do nested conditionals inside a TRIGGER?
much appreciated
although the syntax you have posted is completely wrong, I have figured out what you want try this ( not tested )
DROP TRIGGER IF EXISTS upd_vote;
DELIMITER $$
CREATE TRIGGER upd_vote AFTER UPDATE ON `my_other_table`
FOR EACH ROW BEGIN
IF NEW.vote_value <> OLD.vote_value THEN
SET
NEW.plus_votes = IF(NEW.vote_value=1,OLD.plus_votes+1,OLD.plus_votes),
NEW.minus_votes = IF(NEW.vote_value=1,OLD.minus_votes,OLD.minus_votes +1 )
END IF;
END$$
DELIMITER ;