I want to execute the trigger as long as my first condition is met. But it gives me an error when creating it
DELIMITER $$
CREATE TRIGGER `actualizar`
AFTER UPDATE ON `detalle_ventas`
FOR EACH ROW
BEGIN
IF NEW.estado = 'Cancelado' then
UPDATE articulos SET stock += NEW.cantidad
WHERE articulos.id = NEW.idarticulo
AND articulos.inventariable = 1;
END IF;
END$$
DELIMITER ;
You have to surrond the update with the IF clause if it is canceled
DELIMITER $$
CREATE TRIGGER `actualizar`
AFTER UPDATE ON `detalle_ventas`
FOR EACH ROW
BEGIN
IF NEW.estado = 'Cancelado' then
UPDATE articulos SET stock = stock + NEW.cantidad
WHERE articulos.id = NEW.idarticulo
AND articulos.inventariable = 1;
END IF;
END$$
DELIMITER ;
Related
Would like to create a trigger in mysql need to
update t2.orderstatus as 'inactive' on update of t2.inactivedate and
update t2.orderstatus as 'active' on update of t2.activedate. Tried to adapt below one. but failed
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE TRIGGER trigger_on_stockhistory_update
AFTER update ON stockhistory.inactivedate
FOR EACH ROW
BEGIN
UPDATE mastersku
SET ordernow ='InActive'
WHERE `SSKU` = NEW.SSKU;
END;
$$
DELIMITER;
You can not set trigger on field change. It may be only on table level:
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE
TRIGGER `trigger_on_stockhistory_update` AFTER UPDATE
ON `stockhistory`
FOR EACH ROW BEGIN
IF OLD.inactivedate <> NEW.inactivedate THEN
UPDATE mastersku
SET ordernow ='InActive'
WHERE `SSKU` = NEW.SSKU;
END IF;
IF OLD.activedate <> NEW.activedate THEN
UPDATE mastersku
SET ordernow ='Active'
WHERE `SSKU` = NEW.SSKU;
END IF;
END$$
DELIMITER ;
Or a little elegant query using CASE statement:
DROP TRIGGER IF EXISTS trigger_on_stockhistory_update;
DELIMITER $$
CREATE
TRIGGER `trigger_on_stockhistory_update` AFTER UPDATE
ON `stockhistory`
FOR EACH ROW BEGIN
UPDATE mastersku
SET ordernow = CASE
WHEN OLD.inactivedate <> NEW.inactivedate THEN 'InActive'
WHEN OLD.activedate <> NEW.activedate THEN 'Active'
ELSE ordernow
END
WHERE
SSKU = NEW.SSKU;
END$$
DELIMITER ;
I have a problem with trigger.. I would like to change a value to false if new returnBike is not null.
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
IF (NEW.returnBike <> null) THEN
UPDATE Bike SET isRented = false
WHERE id = (select Bike from Rent); -- but there is Error (missing semicolon)
END IF;
Any advice?
I would be inclined to just do:
DELIMITER $$
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
UPDATE Bike
SET isRented = false
WHERE id = new.Bike AND NEW.returnBike IS NOT NULL;
END; $$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
IF (NEW.returnBike IS NOT NULL) THEN
UPDATE BikeSET isRented = false
WHERE id = new.Bike;
END IF;
END; $$
DELIMITER ;
so to remove problem with syntax semicolon added delimiter and $$ after end;
Anyway thanks to give me some advice with if, comparing!
After insert in the table Aluguel update field Status to busy.
Not work.
DELIMITER $$
CREATE TRIGGER Tgr_Status_Update AFTER INSERT
ON aluguel
FOR EACH ROW
BEGIN
UPDATE apartamento SET status_apart = busy
WHERE id_apart = apartamento_id_apart;
END$$
DELIMITER ;
you have to use new key word
DELIMITER $$ CREATE TRIGGER Tgr_Status_Update AFTER UPDATE
ON aluguel FOR EACH ROW
BEGIN UPDATE apartamento SET status_apart = new.busy
WHERE id_apart = new.apartamento_id_apart;
END$$ DELIMITER ;
---------------------------------------insert trigger -----------------
DELIMITER $$ CREATE TRIGGER Tgr_Status_Insert AFTER INSERT
ON aluguel FOR EACH ROW
BEGIN UPDATE apartamento SET status_apart = new.busy
WHERE id_apart = new.apartamento_id_apart;
END$$ DELIMITER ;
To refer to a column from the table you're inserting into, you need to use NEW.column_name.
And if busy is a string, you need to put it in quotes.
DELIMITER $$
CREATE TRIGGER Tgr_Status_Update AFTER INSERT
ON aluguel
FOR EACH ROW
BEGIN
UPDATE apartamento SET status_apart = 'busy'
WHERE id_apart = NEW.apartamento_id_apart;
END$$
DELIMITER ;
DEMO
I have created a trigger , and this trigger updating the field value , but not checking the if condition ,
DELIMITER $$
create trigger `njsystem`.`test` BEFORE UPDATE on `njsystem`.`tbl_users`
for each row
BEGIN
IF (NEW.user_failed_logins > 3) THEN
UPDATE tbl_users SET user_active = 0;
END IF;
END; $$
DELIMITER ;
What I understand from your problem statement is that, you wanted to set user_active to 0 if user_failed_logins count goes above 3. Here is solution for that, you can also change values using new.
DELIMITER $$
create trigger `test` BEFORE UPDATE on `tbl_users`
for each row
BEGIN
IF (NEW.user_failed_logins > 3) THEN
SET NEW.user_active = 0;
END IF;
END; $$
DELIMITER ;
In addition to the notes table, I am applying this same trigger to around 15 other tables. Not sure, but seems like a stored procedure will eliminate the duplicated code. Is this a good application for a stored procedure? If so, how could it be implemented?
CREATE TRIGGER tg_notes_ins BEFORE INSERT ON notes FOR EACH ROW
BEGIN
IF NEW.created_by_user IS NULL OR NEW.created_by_user = '' THEN
SET NEW.created_by_user = #users_id;
END IF;
IF NEW.modified_by_user IS NULL OR NEW.modified_by_user = '' THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE TRIGGER tg_notes_upd BEFORE UPDATE ON notes FOR EACH ROW
BEGIN
IF NEW.modified_by_user = OLD.modified_by_user THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
I am not claiming this is how to do it, but it seems to work, and would appreciate any opinions. Thanks
DELIMITER $$
CREATE PROCEDURE `createRecord` ()
BEGIN
IF NEW.created_by_user IS NULL OR NEW.created_by_user = '' THEN
SET NEW.created_by_user = #users_id;
END IF;
IF NEW.modified_by_user IS NULL OR NEW.modified_by_user = '' THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE PROCEDURE `modifyRecord` ()
BEGIN
IF NEW.modified_by_user = OLD.modified_by_user THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE TRIGGER tg_notes_upd BEFORE UPDATE ON notes FOR EACH ROW
BEGIN
CALL createRecord();
END$$
CREATE TRIGGER tg_notes_ins BEFORE INSERT ON notes FOR EACH ROW
BEGIN
CALL modifyRecord();
END$$
DELIMITER ;