Error in syntax of MySQL query, cannot figure it out - mysql

Here is my trigger:
DELIMITER //
CREATE TRIGGER trg_pic_hours
AFTER INSERT ON CREW
FOR EACH ROW
BEGIN
IF NEW.CREW_JOB = 'Pilot' THEN
UPDATE PILOT
SET PIL_PIC_HOURS = PIL_PIC_HOURS+(SELECT CHAR_HOURS_FLOWN FROM CHARTER
WHERE CHAR_TRP = NEW.CHAR_TRP)
WHERE EMP_NUM = NEW.EMP_NUM;
END IF;
END;
It errors out on NEW.CHAR_TRP but if I remove the NEW it processes but does not work correctly. Any idea why?

Related

How to fix error code 1415 in MySql workbench when I want to create a trigger?

I got the following error when I try to run my sql code: Error Code: 1415 Not allowed to return a result set from a function
I am trying to create a trigger.
USE `mydb`$$
DROP TRIGGER IF EXISTS `mydb`.`Shops_AFTER_INSERT` $$
USE `mydb`$$
CREATE DEFINER = CURRENT_USER TRIGGER `mydb`.`Shops_AFTER_INSERT` AFTER INSERT ON `Shops`
FOR EACH ROW
BEGIN
DECLARE threshold INT;
SET threshold = 50;
IF (Shops.number_of_sales>threshold) THEN
SELECT concat('We are running out of ', Shops.item_name, 'Please refill stock on')
FROM Shops;
END IF;
END$$

Mysql Trigger Error with IF THEN

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!

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 ;

Why does it say this trigger contains sql syntax errors?

i have written a trigger in mysqlworkbench that should update the product stock after a new line had been added to the order table(my code is in dutch so thats why im explaining this) But it doesn't work, when i try to apply it mysqlworkbench says its sql code contains errors.
Here's the trigger:
CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT` AFTER INSERT ON `bestelregel` FOR EACH ROW
BEGIN
if Product.productnr = NEW.productnr
then
Update Product
Set Hoeveelheid = (Hoeveelheid - NEW.aantal)
Where productnr = NEW.productnr;
end if;
END
What is Product in the if statement? That is, no doubt, generating a syntax error, because it is unrecognized.
I think you intend for the body to be simply:
Update Product p.
Set Hoeveelheid = (Hoeveelheid - NEW.aantal)
Where p.productnr = NEW.productnr;
The if irrelevant.
I would expect the full version to look something like:
DELIMITER $$
CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT`
AFTER INSERT ON `bestelregel` FOR EACH ROW
BEGIN
UPDATE Product p
SET Hoeveelheid = (Hoeveelheid - NEW.aantal)
WHERE p.productnr = NEW.productnr;
END $$
DELIMITER ;
You have delimiter trouble. You need
DELIMITER $$
CREATE ..... TRIGGER ...
BEGIN
END$$
DELIMITER ;

MYSQL Trigger insert or update different table

I've been wrapping my head around this SQL statement I'm trying to make and I've reached a wall so to speak, if you could lend any advise as to what I am doing wrong that would be a big help. I get the error around the 2nd where statement but I dont really expect the last part of the code to work yet.
CREATE TRIGGER UpdateScores
AFTER INSERT ON CharSheet FOR EACH ROW
begin
DECLARE id_exists Boolean;
SELECT
1
INTO
#id_exists
FROM
ScoresTable
WHERE
ScoresTable.PlayerID= NEW.PlayerID;
IF #id_exists = 1
THEN
UPDATE
ScoresTable
SET
Exp = :New.Exp;
WHERE
ScoresTable.PlayerID = :NEW.PlayerID;
END IF;
IF #id_exists = 0
THEN
-- I don't expect this part to work yet.
INSERT INTO ScoresTable VALUES (:New.PlayerID, :New.Race, :New.Class, :New.Exp);
END IF;
END;
$$
The problem is in this block of code:
THEN
UPDATE
ScoresTable
SET
Exp = :New.Exp;
WHERE
ScoresTable.PlayerID = :NEW.PlayerID;
END IF;
Remove the semicolon at the end of the set statement.