Update Data When Trigger Execute in Mysql? - mysql

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

Related

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 ;

How to compare two tables SUM and Update another Table With MySql Trigger

compare two tables SUM and Update another Table With MySql Trigger
DELIMITER $$
CREATE TRIGGER change_com_status_on_sales_table AFTER INSERT ON `commision_calculation` // COMMISSION TABLE
FOR EACH ROW
BEGIN
DECLARE totalQnt1, totalQnt2 DOUBLE;
DECLARE sales_id INT;
SET #sales_id=(SELECT sales_info_id
FROM commercial_invoice WHERE ci_no = NEW.ci_no);
SET #totalQnt1=(SELECT sum(cc.quantity) as total_qnt1
FROM commision_calculation cc
LEFT JOIN commercial_invoice ci ON cc.ci_id = ci.id
WHERE cc.comission_type = 'Remex' AND ci.sales_info_id= #sales_id
GROUP BY #sales_id);
SET #totalQnt2=(SELECT SUM(sii.quantity) as total_income
FROM sales_info_item sii WHERE sii.sales_info_id = #sales_id
GROUP BY #sales_id);
IF (#totalQnt2 > #totalQnt1) THEN
UPDATE sales_information
SET ci_status = 'Partial'
WHERE id = #sales_id;
END IF;
IF (#totalQnt2 = #totalQnt1) THEN
UPDATE sales_information
SET ci_status = 'Full'
WHERE id = #sales_id;
END IF;
END; $$
DELIMITER ;
The error in above as mentioned by Jahid Hossain
I get "SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row"

Dynamic name tablŠµ in trigger

There is a trigger that copies columns from one table to another. How do I assign a table name to a dynamic variable #nametable?
Every day the name of the table changes depending on the current date.
USE dbo;
DROP TRIGGER IF EXISTS `update_test`;
GO
DELIMITER |
CREATE TRIGGER `update_test` AFTER INSERT ON `hello_send`
FOR EACH ROW
BEGIN
DECLARE nametable VARCHAR(128);
SET #nametable =(DATE_FORMAT(NOW(),"%d%m%Y_v"));
INSERT INTO `dbo`.`nametable` SET
`TIME_` =NEW.`recorded`,
`P1` = NEW.`value1`
ON DUPLICATE KEY UPDATE
`TIME_` = NEW.`recorded`,
`P1` = NEW.`value1`;
END;
Have error ERROR 1146: Table 'dbo.nametable' doesn't exist
I decided to do so as a temporary solution:
CREATE TRIGGER `update_test` AFTER INSERT ON `hello_send`
FOR EACH ROW
BEGIN
SET #dateNameTableNOW =(DATE_FORMAT(NOW(),"%d%m%Y_v"));
SET #dateNameTableSELECT1 = '06122017_v';
SET #dateNameTableSELECT2 = '07122017_v';
IF(#dateNameTableNOW = #dateNameTableSELECT1) THEN
INSERT INTO dbo.06122017_v SET TIME_=NEW.recorded, P1 = NEW.value1 ON DUPLICATE KEY UPDATE TIME_ = NEW.recorded, P1 = NEW.value1;
END IF;
IF(#dateNameTableNOW = #dateNameTableSELECT2) THEN
INSERT INTO dbo.07122017_v SET TIME_=NEW.recorded, P1 = NEW.value1 ON DUPLICATE KEY UPDATE TIME_ = NEW.recorded, P1 = NEW.value1;
END IF;
END;

Why this SQL trigger isn't saving any data?

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.

Trigger in mysql updating in another database

is there something wrong with this trigger ? It suppose to update a datefield on a table in another database. Can anyone give me a hand ? thanks
DROP TRIGGER IF EXISTS depoisDeInserir_invoice;
DELIMITER $$
CREATE TRIGGER depoisDeInserir_invoice AFTER INSERT ON pag_invoice
FOR EACH ROW
BEGIN
DECLARE trial datetime;
SET trial = (select dataTrial
from networkcen_1.companyproductuser
where (CompanyKey = NEW.companyKey)
and (ProductKey = NEW.productKey)
and (UserKey = NEW.productKey));
IF (trial < CURDATE()) THEN
UPDATE networkcen_1.companyproductuser
SET dataVencimento = DATE_ADD(CURDATE(),INTERVAL NEW.dias DAY)
where (CompanyKey = NEW.companyKey)
and (ProductKey = NEW.productKey)
and (UserKey = NEW.productKey);
ELSE
UPDATE networkcen_1.companyproductuser
SET dataVencimento = DATE_ADD(trial,INTERVAL NEW.dias DAY)
where (CompanyKey = NEW.companyKey)
and (ProductKey = NEW.productKey)
and (UserKey = NEW.productKey);
END IF;
END;
$$
DELIMITER ;