Error when run query trigger in MySQL - mysql

please help
i have trigger code below
DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN
IF (
SELECT table1.idtable1 FROM table2, table1, table
WHERE table1.idtable1=table2.idtable2
and table0.idtable0=table1.idtable1
)
THEN
UPDATE targettable
SET targettable.column = 1
WHERE targettable.idtable=table1.idtable1;
END IF;
END$$
after running it, it shows 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 17
how to fix it? MySQL version is 5.5.34

table is a reserved word in mysql. Surround table with backticks. Also you were missing delimiter and a semicolon at the end.
This works:
DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN
IF (
SELECT table1.idtable1 FROM table2, table1, `table`
WHERE table1.idtable1=table2.idtable2
and table0.idtable0=table1.idtable1
)
THEN
UPDATE targettable
SET targettable.column = 1
WHERE targettable.idtable=table1.idtable1;
END IF;
END;$$
delimiter ;
I hope that helps!

DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN
IF (
SELECT table1.idtable1 FROM table2, table1, table <-- you should escape this using backticks as this is a reserved word i.e., `table`
WHERE table1.idtable1=table2.idtable2
and table0.idtable0=table1.idtable1
)
THEN
UPDATE targettable
SET targettable.column = 1
WHERE targettable.idtable=table1.idtable1;
END IF;
END$$

Related

#1064 - You have an error in your SQL syntax at line 7

enter code here
DELIMITER $$
DROP TRIGGER IF EXISTS `Update_Status`$$
CREATE TRIGGER `Update_Status` AFTER INSERT ON `occurance_time`
FOR EACH ROW BEGIN
IF NOT EXISTS (SELECT `F_Seen` FROM `Total_Hours` WHERE (`SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`))
THEN
INSERT INTO `total_time` (`SSN`,`Name`,`Day_Date`,`F_Seen`) VALUES(new.`SSN`,new.`Name`,new.`Day_Date`,new.`Cap_time`);
ELSE
UPDATE `total_time` SET(`L_Seen`=new.`Cap_time`) WHERE (`SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`);
END$$
I have Created this After insert trigger On Occurrence _time I want to store first time of occurrence of a day and last time of occurrence of Day in Total_time table but getting this 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 '(L_Seen=new.Cap_time) WHERE (SSN=new.SSN and Day_Date=new.`Day_Date...' at line 7" ***
Instead of SET(L_Seen=new.Cap_time) Please use SET L_Seen=new.Cap_time. You missed end if also. Below code is working.
CREATE TRIGGER `Update_Status` AFTER INSERT ON `occurance_time`
FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT `F_Seen` FROM `Total_Hours` WHERE `SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`)
THEN
INSERT INTO `total_time` (`SSN`,`Name`,`Day_Date`,`F_Seen`) VALUES(new.`SSN`,new.`Name`,new.`Day_Date`,new.`Cap_time`);
ELSE
UPDATE `total_time` SET `L_Seen`=new.`Cap_time` WHERE `SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`;
end if;
end
DB-Fiddle:
create table occurance_time(SSN varchar(50),Name varchar(50),Day_Date date,Cap_time time);
CREATE TRIGGER Update_Status AFTER INSERT ON occurance_time
FOR EACH ROW BEGIN
IF NOT EXISTS (SELECT F_Seen FROM total_time WHERE SSN=new.SSN and Day_Date=new.Day_Date)
THEN
INSERT INTO total_time (SSN,Name,Day_Date,F_Seen) VALUES(new.SSN,new.Name,new.Day_Date,new.Cap_time);
ELSE
UPDATE total_time SET L_Seen=new.Cap_time WHERE SSN=new.SSN and Day_Date=new.Day_Date;
end if;
end
db<>fiddle here

MySQL insert query if condition matches in after insert trigger

I'm using WampServer with MySQL Version: 5.7.9 and phpMyAdmmin Version: 4.5.2
I'm trying to write an after insert trigger on Table1 to insert data in Table2 if the data doesn't exist in Table2. Below is my query:
DROP TRIGGER IF EXISTS `insert_customer`;
CREATE TRIGGER `insert_customer` AFTER INSERT ON `installations`
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM `customers` WHERE `customers`.`phone` = NEW.`phone`) = 0
THEN INSERT INTO `customers` (`phone`, `imei`, `platform`, `version_code`) VALUES (NEW.`phone`, NEW.`imei`, NEW.`platform`, NEW.`version_code`);
END IF;
END;
This gives me 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 4
I've tried with delimiter too:
DROP TRIGGER IF EXISTS `insert_customer`;
DELIMITER |
CREATE TRIGGER `insert_customer` AFTER INSERT ON `installations`
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM `customers` WHERE `customers`.`phone` = NEW.`phone`) = 0
THEN INSERT INTO `customers` (`phone`, `imei`, `platform`, `version_code`) VALUES (NEW.`phone`, NEW.`imei`, NEW.`platform`, NEW.`version_code`)
END IF;
END;
|
DELIMITER ;
This gives me 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 'END IF;
END' at line 5
What am I doing wrong here? Any help?
Finally I've got it. The correct SQL is:
DROP TRIGGER IF EXISTS `insert_customer`;
DELIMITER |
CREATE TRIGGER `insert_customer` AFTER INSERT ON `installations`
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM `customers` WHERE `customers`.`phone` = NEW.`phone`) = 0 THEN
INSERT INTO `customers` (`phone`, `imei`, `platform`, `version_code`) VALUES (NEW.`phone`, NEW.`imei`, NEW.`platform`, NEW.`version_code`);
END IF;
END;|
DELIMITER ;
The ; at the end of my INSERT query solved my problem. :D
My guess is the extra delimiter; try changing
END;
|
to
END|

MySQL before insert trigger filling omitted column

I am trying to make make a trigger, that will fill column B with value from column A if column B was not explicitly set in insert query. (column B is set to allow NULL and to default to NULL value)
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table FOR EACH ROW
BEGIN
IF (NEW.valueB IS NULL) THEN SET NEW.valueB = NEW.valueA ;
END
$$
But I am getting this error (not very helpful).
ERROR 1064 (42000): 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
When I tried to locate this problematic empty string '' making the query like one word per line, mysql marked the line with '=' character as problematic.
I double checked the query for any non-ascii characters.
I am using mysql version 5.5.37-0ubuntu0.12.10.1 through commandline (eg not phpmyadmin).
You need to close the IF with END IF
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF NEW.valueB IS NULL THEN
SET NEW.valueB = NEW.valueA ;
END IF ;
END;$$
delimiter ;
Check the example here http://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
You forgot to close the IF statement. Read the syntax here. I think the code below will work for you.
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF (NEW.valueB IS NULL) THEN
SET NEW.valueB = NEW.valueA ;
END IF
END $$
DELIMITER

Trigger creation query failed

I have three tables, when data is inserted into third table, I am checked whether newly inserted row has field value in table1. If value exists then I am moving the same row from table3 to table2. My code is like below, But I am getting error while executing this
DELIMITER $$
CREATE TRIGGER onRosterAdd
AFTER INSERT ON yet_tobe
FOR EACH ROW
BEGIN
SELECT CASE WHEN (SELECT 1 FROM users WHERE NEW.jid = (users.username + "_1")) > 0
THEN
INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
END
END$$
DELIMITER ;
And here is the error
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 'INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
How can I fix this.
Try it like this:
DELIMITER $$
CREATE TRIGGER onRosterAdd
BEFORE INSERT ON yet_tobe
FOR EACH ROW
BEGIN
IF (SELECT 1 FROM users WHERE CONCAT(username,"_1")=NEW.jid)>0
THEN
INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
END IF;
END$$
DELIMITER ;
Here's a working fiddle

Perform insert or update on table using trigger

I am trying to fetch some values from php page and perform trigger in mysql database. Which is as below:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `test`.`MysqlTrigger`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `MysqlTrigger` AFTER INSERT OR UPDATE ON `table2`
FOR EACH ROW BEGIN
IF(NEW .flag=0) THEN
INSERT INTO temp(sent,pcount,ncount) VALUES (NEW.sent,NEW.pcount,NEW.ncount);
ELSE
UPDATE temp SET pcount=NEW.pcount AND ncount=NEW.ncount WHERE id = NEW.id;
END IF;
END;
$$
DELIMITER ;
Which gives error:
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 'or update ON `table2`
FOR EACH ROW BEGIN
if(NEW .flag=0) then
INSERT INTO' at line 3
I tried to change = with =: but error persist. Any idea whaat the prob?
You can't define a trigger for update and insert at the same time. You have to define 2 triggers:
CREATE TRIGGER `Mysql_insert_Trigger` AFTER INSERT ON `table2` ...
CREATE TRIGGER `Mysql_update_Trigger` AFTER UPDATE ON `table2` ...
I count do it. There was problem in syntax. Here is the correct which perform update insert at same moment in different table and update-inset on same table but with if else:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `test`.`MysqlTrigger2`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `MysqlTrigger2` AFTER INSERT ON `table2`
FOR EACH ROW BEGIN
IF 'flag'=1 THEN
UPDATE record SET sent=NEW.sent WHERE id=NEW.id;
ELSE
INSERT INTO record VALUES (id,NEW.sent);
INSERT INTO temp VALUES ()
END IF;
END;
$$
DELIMITER ;