Mysql Trigger Error with IF THEN - mysql

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!

Related

create a trigger need to update table 2 on update of table1

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 ;

How to use select query in stored procedure from trigger in mysql

Here is my Trigger
DELIMITER $$
CREATE TRIGGER before_insert BEFORE INSERT ON SampleTable
FOR EACH ROW
BEGIN
IF NEW.flag =0 THEN
SET NEW.media = "../trigger/smile1.png";
ELSEIF NEW.flag = 1 THEN
SET NEW.media = "../trigger/smile2.png";
ELSEIF NEW.flag = 2 THEN
SET NEW.media = "../trigger/smile3.png";
END IF;
CALL Media (NEW.flag);
END$$
DELIMITER ;
The trigger was created successfully. And I called stored procedure from a trigger.
procedure
DELIMITER $$
CREATE PROCEDURE Media (a INT)
BEGIN
SELECT firstname, lastname, media FROM SampleTable WHERE flag = a;
END $$
DELIMITER ;
When I use insert query it shows the following error.
not allowed to return a result set from a trigger
I found select query is a reason for the error. But I don't know how to solve it.
Any Help...

MySQL need help setting up trigger

I am trying to set up a TRIGGER to clear empty strings before INSERT. Not rocket science but I can't find the error in my syntax!
Here is the TRIGGER itself
USE `ga_abtest_logging`;
DELIMITER $$
CREATE TRIGGER avoid_empty
BEFORE INSERT ON daily_analytics
FOR EACH ROW
BEGIN
IF profileID = ''
THEN SET profileID = NULL
END IF;
END$$
Here is what workbench is showing:
On hover over the END IF it reads syntax error, unexpected END, expecting ';'
Could I have a problem with the settings on my DB? I have gone through the MySQL docs and I think the trigger looks right! Does anyone see anything obviously wrong?
You should make a few changes:
Use the NEW. prefix when referencing a column value
Add a semi-colon at the end of the line where you set the value
For example:
DELIMITER $$
CREATE TRIGGER tr_b_ins_daily_analytics BEFORE INSERT ON daily_analytics FOR EACH ROW BEGIN
IF (NEW.profileID = '')
THEN
SET NEW.profileID = NULL;
END IF;
END $$
DELIMITER ;
The code below should work.
DELIMITER $$
CREATE TRIGGER avoid_empty
BEFORE INSERT ON daily_analytics
FOR EACH ROW
BEGIN
IF NEW.profileID = ''
THEN SET NEW.profileID = NULL;
END IF;
END;$$
DELIMITER ;

Trigger not checking if condition

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 ;

MYSQL trigger error 1064

I would like to check if a record is being created with a user id, if not then generate one. I can't figure out what I'm doing wrong?
delimiter $$
DROP TRIGGER IF EXISTS init_uuid_users;
CREATE TRIGGER init_uuid_users BEFORE INSERT ON `users`
FOR EACH ROW BEGIN
IF (NEW.username IS NULL) THEN
SET NEW.username = UUID();
END IF;
END;
$$
delimiter ;
DROP TRIGGER IF EXISTS init_uuid_users;
put it before delimiter.
And remove ; after END.