Syntax error when creating a MySQL trigger - mysql

I have some problems trying to do a trigger.
Here is my MySQL query for the trigger:
delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN (
SELECT round(2*livello*livello + 13.8*livello)
FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
WHILE (temp%6<>0) temp=temp+1;
END WHILE;
UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
)
END IF;
END //
And the error is
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; WHILE (temp%6<>0) temp=temp+1; END WHILE; UPDATE strutture SET ' at line 8
Does someone have an idea why I'm getting this error?

Remove the parentheses around your THEN clause. Also your WHILE clause is syntactically incorrect:
delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN
SELECT round(2*livello*livello + 13.8*livello)
FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
WHILE temp%6<>0 DO
SET temp=temp+1;
END WHILE;
UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
END IF;
END //

Related

Getting 1064 error while creating mysql trigger

I am trying to create mysql trigger but getting an error
Error #1064 - You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near '' at line 14
Here is my sql script
delimiter $$
CREATE TRIGGER `update_account_balance_after_transfer` AFTER INSERT ON `balance_transfer`
FOR EACH ROW
BEGIN
IF(NEW.from_account_type='CUSTOMER') THEN
UPDATE customer c SET c.balance = c.balance-NEW.amount WHERE c.customer_id= NEW.from_account_id;
ELSE IF(NEW.from_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance-NEW.amount WHERE v.vendor= NEW.from_account_id;
END IF;
IF(NEW.to_account_type='VENDOR') THEN
UPDATE customer c SET c.balance = c.balance+NEW.amount WHERE c.customer_id= NEW.to_account_id;
ELSE IF(NEW.to_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance+NEW.amount WHERE v.vendor= NEW.to_account_id;
END IF;
END $$
delimiter ;
Remove space between ELSE and IF. It should be ELSEIF as below.
delimiter $$
CREATE TRIGGER `update_account_balance_after_transfer` AFTER INSERT ON `balance_transfer`
FOR EACH ROW
BEGIN
IF(NEW.from_account_type='CUSTOMER') THEN
UPDATE customer c SET c.balance = c.balance-NEW.amount WHERE c.customer_id= NEW.from_account_id;
ELSEIF(NEW.from_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance-NEW.amount WHERE v.vendor= NEW.from_account_id;
END IF;
IF(NEW.to_account_type='VENDOR') THEN
UPDATE customer c SET c.balance = c.balance+NEW.amount WHERE c.customer_id= NEW.to_account_id;
ELSEIF(NEW.to_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance+NEW.amount WHERE v.vendor= NEW.to_account_id;
END IF;
END $$
delimiter ;
MySQL IF Syntax
Check the demo here

Facing Error while creating a MYSQL trigger

DELIMITER $$
DROP TRIGGER IF EXISTS TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE $$
CREATE TRIGGER TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE
BEFORE UPDATE ON USERS
FOR EACH ROW
BEGIN
IF #DISABLE_TRIGGERS IS NULL THEN
IF NOT (NEW.MANUALEXPIRATIONDATE <=> OLD.MANUALEXPIRATIONDATE) THEN
SET NEW.CUSTOMFIELD2 = NEW.MANUALEXPIRATIONDATE;
IF NOT(NEW.USEREXPIRYDATE <=> OLD.USEREXPIRYDATE) THEN
SET NEW.CUSTOMFIELD3 = NEW.USEREXPIRYDATE;
END IF;
END IF;
END$$
DELIMITER ;
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 17
CREATE TRIGGER TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE
BEFORE UPDATE ON USERS
FOR EACH ROW
BEGIN
IF #DISABLE_TRIGGERS IS NULL THEN
IF NOT (NEW.MANUALEXPIRATIONDATE <=> OLD.MANUALEXPIRATIONDATE) THEN
SET NEW.CUSTOMFIELD2 = NEW.MANUALEXPIRATIONDATE;
END IF;
IF NOT(NEW.USEREXPIRYDATE <=> OLD.USEREXPIRYDATE) THEN
SET NEW.CUSTOMFIELD3 = NEW.USEREXPIRYDATE;
END IF;
END IF;
END
$$

error in DECLARE variable mysql

this is my trigger
CREATE TRIGGER `proximo_pago` BEFORE INSERT ON `pago`
FOR EACH ROW BEGIN
declare num_orden integer;
select max(orden) from pago where lote=NEW.lote into num_orden;
if(num_orden is NULL)
then
set NEW.orden=1;
else
set NEW.orden=num_orden+1;
end if;
END
and the ERROR
SQL query:
CREATE TRIGGER `proximo_pago` BEFORE INSERT ON `pago`
FOR EACH ROW BEGIN
declare num_orden integer;
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
I've tried to use DELIMITER but it isnĀ“t works, please help me , thanks
My answer is based only on the necessity of your question which is declaration of the variable.. if the Delimiter doesn't do the trick maybe its an error in your syntax because you didn't SET your SELECT query in a variable. try this first.
DELIMITER $$
CREATE
TRIGGER `proximo_pago` BEFORE INSERT
on `pago`
FOR EACH ROW
BEGIN
DECLARE num_orden int;
SET num_orden = select max(orden) from pago where lote=NEW.lote;
if(num_orden is NULL)
then
set NEW.orden=1;
else
set NEW.orden=num_orden+1;
end if;
END$$
DELIMITER ;

What is wrong with this MySQL Trigger that prevents null dates?

I am trying to create this trigger to prevent insertion of null dates:
CREATE TRIGGER responses_before_insert BEFORE INSERT ON responses
FOR EACH ROW
BEGIN
IF (NEW.date_of_plan IS NULL OR NEW.date_of_plan = '0000-00-00') THEN
SET NEW.date_of_plan = CURDATE();
END IF;
IF (NEW.date_of_update IS NULL OR NEW.date_of_update = '0000-00-00') THEN
SET NEW.date_of_update = CURDATE();
END IF;
END
However, I get the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5
Can anyone explain what is wrong?
Thanks!
The issue was resolved by changing the delimiter before the query, ie. I executed
DELIMITER $$
Before the trigger query.
Like you did, you need a delimiter.
DELIMITER $$
CREATE TRIGGER responses_before_insert BEFORE INSERT ON responses
FOR EACH ROW
BEGIN
IF (NEW.date_of_plan IS NULL OR NEW.date_of_plan = '0000-00-00') THEN
SET NEW.date_of_plan = CURDATE();
END IF;
IF (NEW.date_of_update IS NULL OR NEW.date_of_update = '0000-00-00') THEN
SET NEW.date_of_update = CURDATE();
END IF;
END $$
DELIMITER ;
Then you can also set it back as done on the last line. Better practie!

MySQL trigger: error when checking if variable exists

I have a trigger which references a global variable that's coming from a delete query which sets the trigger off:
DELIMITER ;;
CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW
BEGIN
IF (SELECT #userID IS NULL)
THEN #userID := 0
END IF;
END
;;
DELIMITER ;
For some reason I'm getting SQL error:
SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '#userID := 0
I don't understand this, it's a very basic IF statement, why doesn't it work?
UPDATE: I'm totally confused, I've tried this
IF TRUE
THEN TRUE
END IF;
and it still throws an error... Seriously?
UPDATE 2: SOLVED This actually works, however it is very weird
IF (SELECT #userID IS NULL)
THEN SET userID = 0;
END IF;
Apparently you need a semicolon inside IF statement?
CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW
BEGIN
IF (SELECT #userID IS NULL)
THEN
SET #userID = 0;
END IF;
END;