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 ;
Related
I want WaterRateMeterStart with WaterRateMeterEnd have same value sequentially if data updated.
This is my table
And This is my table structure
This my code and
I do not know how mistakes can occur.
DELIMITER $$
CREATE TRIGGER after_update_rates_water_again AFTER UPDATE ON waterrates FOR EACH ROW
BEGIN
IF
new.WaterRateMeterStart <> old.WaterRateMeterStart THEN
UPDATE waterrates
SET WaterRateMeterEnd = new.WaterRateMeterStart
WHERE
WaterRateId = ( new.WaterRateId - 1 );
ELSEIF new.WaterRateMeterEnd <> old.WaterRateMeterEnd THEN
UPDATE waterrates
SET WaterRateMeterStart = new.WaterRateMeterEnd
WHERE
WaterRateId = ( new.WaterRateId + 1 );
END IF;
END $$
DELIMITER ;
And error if I updated the data
UPDATE `waterrates` SET `WaterRateMeterStart` = '9' WHERE `waterrates`.`WaterRateId` = 2
I'm trying to get a trigger to run before an update is processed on a per record level of a table.
What I'm trying to do is when an update occurs on a record for the qst_title column, if the column qst_perma_title is NULL then the qst_perma_title column is updated with the value in the qst_title column and the qst_title column is updated as per the original update query.
I'm using the code below but no errors are shown and the column qst_perma_title isn't updated.
The current values are:
qst_title = 'Old Title'
qst_perma_title = NULL
query
UPDATE TD_QUESTION SET qst_title = 'New Title Value' WHERE qst_id = 1;
trigger
DELIMITER //
CREATE TRIGGER TRG_QST_UPD
BEFORE UPDATE ON TD_QUESTION
FOR EACH ROW
BEGIN
IF OLD.`qst_perma_title` IS NULL THEN
SET NEW.`qst_perma_title` = OLD.`qst_title`;
END IF;
END//
DELIMITER ;
CREATE TRIGGER TRG_QST_UPD
BEFORE UPDATE ON TD_QUESTION
FOR EACH ROW
IF OLD.qst_perma_title IS NULL THEN
SET NEW.qst_perma_title = OLD.qst_title;
END IF;
First run this query and then try updating. I checked, it is working fine for me.
I want to add a trigger if name has update with "NEW_" string. Below is my current trigger but its working without checking the updated value contain "NEW_" . please advice
if user updates name from "Ann" to "NEW_Ann" trigger should work.
if user updates name from "Ann" to "Ann111" trigger shouldn't work
Code:
$this->execute("ALTER TABLE `users`
ADD COLUMN `date_` TIMESTAMP NULL DEFAULT NULL");
$this->execute("CREATE TRIGGER `update_user_deleted_date` BEFORE UPDATE ON `users` FOR EACH ROW BEGIN
IF (NEW.name!= OLD.name) THEN
SET NEW.date_deleted = NOW();
END IF;
END ;");
Use this:
CREATE TRIGGER update_user_deleted_date BEFORE UPDATE ON users
FOR EACH ROW BEGIN
IF (LOCATE('NEW_', NEW.name) != 0) THEN
SET NEW.date_deleted = NOW();
END IF;
END;
LOCATE('NEW_', NEW.name) != 0 means that the location of string "NEW_" is found
I have the following trigger
CREATE TRIGGER `qc_date_trigger` AFTER UPDATE ON `brand`
FOR EACH ROW BEGIN
IF NEW.brandQC = '1' THEN
SET #brandQCDate = CURDATE();
ELSE
SET #brandQCDate = NULL;
END IF;
END
For some reason it does not update my Date field into the current date when QC is = 1. I've checked the mysql doc and it should work. Any ideeas?
If your column is named brandQCDate, then remove the #. The # makes it a user defined variable, not the column. Also you want to make this a BEFORE UPDATE trigger.
CREATE TRIGGER `qc_date_trigger` BEFORE UPDATE ON `brand`
FOR EACH ROW BEGIN
IF NEW.brandQC = '1' THEN
SET NEW.brandQCDate = CURDATE();
ELSE
SET NEW.brandQCDate = NULL;
END IF;
END
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 ;