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;
Related
I'm trying to create a trigger on a table, but I keep getting an error. Any idea what's wrong with the following statement?
CREATE TRIGGER `some_name` BEFORE UPDATE ON `some_table`
FOR EACH ROW BEGIN
IF NEW.isDeleted = 1 THEN
SET NEW.isSearchable = 0;
ELSE THEN
SET NEW.isSearchable = 1;
END IF;
END;
Mysql output:
#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 4
You are missing delimiter and also no need of then after else
delimiter //
create trigger `some_name` BEFORE UPDATE ON `some_table`
for each row
begin
if new.isDeleted = 1 then
SET NEW.isSearchable = 0;
else
SET NEW.isSearchable = 1;
end if;
end;//
delimiter ;
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
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!
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 //
I am trying to write trigger in Mysql (5.1), but getting following error, please help.
The error is:
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 '' at line 5.
Purpose for writing trigger:
I am writing application where I am assigning users, and I want to store unassigned usercount to field cluster_count in IX_branchdetails table.After updating the base table.
trigger:
DELIMITER $$
CREATE TRIGGER upd_trg AFTER
UPDATE ON DBNAME.BASETABLE
FOR EACH ROW
BEGIN
DECLARE m_branchcode INTEGER;
DECLARE cnt INTEGER;
DECLARE cursor_branch CURSOR FOR
SELECT DISTINCT branchcode
FROM ix_branchdetails;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cursor_branch;
my_loop: loop
set done = false;
fetch cursor_branch into m_branchcode;
if done then
leave my_loop;
end if;
select count(1) into cnt from (select count(1) from BASETABLE Where IX_BRANCHCODE = m_branchcode) as temp;
update DBANAME.ix_branchdetails set DBANAME.ix_branchdetails.cluster_count = cnt where DBANAME.ix_branchdetails.BRANCHCODE = m_branchcode;
end loop my_loop;
close cursor_branch;
END $$
DELIMITER ;
I don't see a declare for the done variable:
DECLARE done TINYINT DEFAULT FALSE;
The semicolon (;) is the default delimiter for MySQL statements. To get a procedure/function/trigger defined, we normally see the statement delimiter changed to a string that doesn't appear in the statement:
DELIMITER $$
CREATE PROCEDURE ...
END$$
DELIMITER ;
If the delimiter is not changed from the semicolon, then when MySQL encounters the first semicolon in your procedure/function/trigger, it sees that as the end of the statement, which is not what you want. You want MySQL to see the entire block of code as a single statement.