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

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!

Related

Syntax error during creation of trigger on mySQL 10.3.17-MariaDB-log version

I'm trying to add a trigger that should automatically update a datetime column only if is not present a SQL variable. The trigger code is the following
delimiter |
CREATE TRIGGER update_sync_date
BEFORE UPDATE ON posts
FOR EACH ROW
BEGIN
IF #disable_sync_date_update IS NULL OR #disable_sync_date_update <> 1 THEN
SET new.sync_date = CURRENT_TIMESTAMP
END IF
END|
delimiter ;
The error that I recieve is
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END IF
END' at line 7
Thanks for helping
I've found the solution, here is the correct code:
delimiter |
CREATE TRIGGER update_sync_date
BEFORE UPDATE ON posts
FOR EACH ROW
BEGIN
IF #disable_sync_date_update IS NULL OR #disable_sync_date_update <> 1 THEN
SET new.sync_date = CURRENT_TIMESTAMP;
END IF;
END|
delimiter ;

SQL Trigger Creation - How do I check if NULL or empty?

I'm having some issues with my SQL syntax it seems.
I'm trying to create a trigger and then check if data is NULL or empty.
This is the code now thanks to Bill & Gordon:
DROP TRIGGER IF EXISTS `tablename_OnInsert`;
DELIMITER $$
CREATE TRIGGER `tablename_OnInsert` BEFORE INSERT ON `users`
FOR EACH ROW
BEGIN
IF NEW.`swid` IS NULL OR NEW.`swid` = '' THEN
SET NEW.`swid` = CONCAT('{', uuid(), '}');
END IF
END$$
;
The server still responds with an 1064:
/* 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 'END' at line 7 */
I've been looking around to see what I am doing wrong here, but I just don't get it.
Well:
DROP TRIGGER IF EXISTS `tablename_OnInsert`;
DELIMITER $$
CREATE TRIGGER `tablename_OnInsert` BEFORE INSERT ON `users`
FOR EACH ROW
BEGIN
IF NEW.`swid` IS NULL OR NEW.`swid` = '' THEN
SET NEW.`swid` = CONCAT('{', uuid(), '}');
END IF;
END$$
Your problem appears to be the extra ) in the IF. However, I recommend BEGIN/END and setting the delimiter.

MYSQL Trigger Get Last Row Value and Set

I have been having a problem with this one. Granted this is the first trigger I have ever made (pretty new to this). I think it's a formatting issue. I have a table called filleradown. I need for every time a record is inserted to check if the value of B3_4_5 in the new row is a 1. If it is I need it to then run a select to find the last entry that B3_4_5 was a 0. Then update the NEW.stoptime value to be the 'time' value of the last 0 record.
I think I have all the parts there, but can't seem to get it to run. Please help.
DELIMITER $$
CREATE TRIGGER downinsert
BEFORE INSERT ON 'filleradown' FOR EACH ROW BEGIN
DECLARE downtime DATETIME;
IF (NEW.B3_4_5 = '1') THEN
SELECT time INTO downtime FROM filleradown WHERE B3_4_5 = 0 ORDER BY time DESC LIMIT 1;
SET NEW.stoptime = downtime;
END IF
$$
DELIMITER ;
UPDATE: I got the code a little better here, but still getting some errors.
DELIMITER $$
CREATE TRIGGER downinsert BEFORE INSERT ON filleradown
FOR EACH ROW
BEGIN
DECLARE dt DATETIME;
IF NEW.B3_4_5 = '1' THEN
SELECT MAX(time) INTO dt FROM filleradown WHERE filleradown.B3_4_5 = 0;
SET NEW.stoptime = dt;
END IF
$$
DELIMITER ;
I get the error
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 8
Got it fixed. Was literally missing the END statement. facepalm
DELIMITER $$
CREATE TRIGGER downinsert BEFORE INSERT ON filleradown
FOR EACH ROW
BEGIN
DECLARE dt DATETIME;
IF NEW.B3_4_5 = '1' THEN
SELECT MAX(time) INTO dt FROM filleradown WHERE filleradown.B3_4_5 = 0;
SET NEW.stoptime = dt;
END IF;
END $$
DELIMITER ;

Error SQL syntax in a trigger, MySQL

I receive this message "#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 6" but can not figure out what is wrong.
(position and points are MEDIUMINT, they are not primary key neither unique)
Anyone?
CREATE TRIGGER pointsAssigns
before INSERT ON MyTable
FOR EACH ROW
BEGIN
IF NEW.position>6 THEN
set NEW.points=5;
END IF;
END;
As #Mihai mentioned either add closing END and change the DELIMITER
DELIMITER //
CREATE TRIGGER pointsAssigns
BEFORE INSERT ON MyTable
FOR EACH ROW
BEGIN
IF NEW.position > 6 THEN
SET NEW.points = 5;
END IF;
END //
DELIMITER ;
Here is a SQLFiddle demo
or make it one-line trigger and then you don't need neither BEGIN...END block nor changing the DELIMITER
CREATE TRIGGER pointsAssigns
BEFORE INSERT ON MyTable
FOR EACH ROW
SET NEW.points = IF(NEW.position > 6, 5, NEW.points);
Here is a SQLFiddle demo

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;