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.
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
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;
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 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 ;
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 //