Mysql Trigger creation query - Syntax error - mysql

I am getting an error in my mySql query when i execute my create trigger query in mysql using phpmyadmin which is provided below.
Error:
Error 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 5
MySql Query:
CREATE TRIGGER Trigger_INSERT_club_app_member_contact_info After INSERT ON club_app_member_contact_info
FOR EACH ROW
BEGIN
INSERT INTO club_app_events_list(type_id,event_title,event_description,event_date,event_time,event_venue)
values(1,CONCAT('Happy Birthday ',NEW.title,NEW.name),NEW.mobile_no,NEW.dob,'8.30AM','');
IF NEW.ismarried == 'Yes' THEN
INSERT INTO club_app_events_list(type_id,event_title,event_description,event_date,event_time,event_venue)
values(1,CONCAT('Happy Birthday ',NEW.title_for_spouse,NEW.nameOf_spouse,'Spouse of (',NEW.title,NEW.name,')'),NEW.spouse_mobileNo,NEW.spouse_dob,'8.30AM','');
INSERT INTO club_app_events_list(type_id,event_title,event_description,event_date,event_time,event_venue)
values(2,CONCAT('Happy Wedding Anniversary to ',NEW.title,NEW.name ,' & ',NEW.title_for_spouse,NEW.nameOf_spouse),NEW.mobile_no,NEW.weeding_date,'8.30AM','');
END IF
END
I don't know what's wrong with my query.Can anyone help me to find a solution.

Few thing i noticed IF NEW.ismarried == 'Yes' THEN it should IF NEW.ismarried = 'Yes' THEN BEGIN and END IF should end with semicolon ; although BEGIN end enclose END;
But since i am trying to execute this query using phpmyadmin I have to remove Delimiter from the bottom text box of phpmyadmin.
But note that this is only phpmyadmin analysis warnings not a mysql server response.

Related

Mysql: ER_PARSE_ERROR: while sending create procedure code from node js

I am trying to send create procedure query from node js to MYSQL.
I am reading Mysql.proc table to get the stored procedure definitions.
Mysql code working fine when submitting from mysql client like workbench or hedis
Getting following error while submitting code from NodeJS
{ Error: ER_PARSE_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 'CREATE PROCEDURE `qwe`.`USP_GET_ALL_STOCK_WITH_INDICATORS`( IN `IPV_DATE` DATE )' at line 1
.
.
.
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage:
'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 \'CREATE PROCEDURE `qwe`.`USP_GET_ALL_STOCK_WITH_INDICATORS`( IN `IPV_DATE` DATE )\' at line 1',
sqlState: '42000',
index: 0,
sql:
'USE qwe; CREATE PROCEDURE `qwe`.`USP_GET_ALL_STOCK_WITH_INDICATORS`( IN `IPV_DATE` DATE )BEGIN IF NOT EXISTS(SELECT 1 FROM StockCandle where `DATE`=IPV_DATE) THEN SET #D:=(SELECT MAX(`DATE`) FROM StockCandle); ELSE SET #D:=IPV_DATE; END IF; -- select #D; SELECT SM.Symbol, SM.MA13, SM.MA8, SM.MA5, CASE WHEN SM.MA5>SM.MA8 AND SM.MA8>MA13 THEN \'>>>\' WHEN SM.MA5<SM.MA8 AND SM.MA8<MA13 THEN \'<<<\' ELSE NULL
END ALLIGATOR ,SM.RSI, SM.BHAVTIME, SM.CANDLEINDICATOR FROM StockMaster SM where SM.DATE=#D; END // ' }
tried below code to pares sql in node
formattdSQL = formattdSQL.replace(/(?:\\[rtn]|[\r\t]+)+/g, ' ');
Query to get procedure
SELECT `name`, CONVERT(param_list USING utf8), CONVERT(body USING utf8)
INTO #spname, #spparams, #spbody
FROM mysql.proc WHERE `name` = 'USP_GET_ALL_STOCK_WITH_INDICATORS' AND db = v_oldDB;
SET #sql = CONCAT(#sql, '\r\n', 'DELIMITER //','\r\n','CREATE PROCEDURE `', v_newDB, '`.`', #spname, '`(', #spparams,')',#spbody, ' //', '\r\n','');
I expect that create procedure query should be executed successfully from node
The DELIMITER directive is not part of MySQL Server SQL. That is used only with interactive client utilities that parse an input stream or the contents of an on-screen text box into individual statements -- like workbench or (presumably) "hedis" (whatever that may be).
You don't use it when you are using a programming library to send a query.
Just send the procedure declaration as a single query. Send the USE statement as a separate query, before that.
First query:
USE qwe
(A trailing semicolon in the first query is not actually expected by the server but is allowed if you send it.)
Second query:
CREATE PROCEDURE ...
...
END
There should be no DELIMITER // before, nor // after. Those are all client-side constructs, not meaningful to the server.
There is no need to use your formattdSQL.replace(...) statement to remove newlines. That only serves to make your code unreadable, and isn't needed.

simple mysql trigger, cannot find the syntax error. appreciate for it

I am using phpmyadmin and got a syntax error from my trigger part. I followed examples online but didn't see anything wrong. Does anyone see the syntax error? Thank you in advance.
Here is my code:
CREATE TRIGGER INSERT_T BEFORE INSERT ON dataT
for each row begin
IF(NEW.id IS NOT NULL)
SET NEW.id = 9999;
END IF
end;
mysql reports that:
#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 'SET NEW.id = 9999' at line 4
.
Missing THEN keyword.
MySQL Reference Manual:
IF Syntax https://dev.mysql.com/doc/refman/5.7/en/if.html
And for your next questions. Yes, you need a semicolon following the END IF
And you need to specify a delimiter other than the default semicolon. e.g.
DELIMITER $$
CREATE TRIGGER ...
END$$
DELIMITER ;

Error on SQL query right syntax to use near '' at line 4

i'm trying to create a trigger on my database but i'm getting this 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 '' at line 4 " but i can't understand why. This is the query:
CREATE TRIGGER cancella_associativa AFTER DELETE ON libro
FOR EACH ROW
BEGIN
DELETE FROM annuncio_autore
WHERE libro.`idLibro` = annuncio_autore.`idAnnuncio`;
END
Using phpMyAdmin, thanks in advance.
The ; after the line that contains WHERE ends your (incomplete) CREATE TRIGGER query. You need to change the delimiter:
delimiter //
CREATE TRIGGER ... //
delimiter ;
For more information see MySQL manual.

mysql trigger code deploying from TOAD but not from myphpadmin or from mysql console

I have a very simple trigger. I cretaed it from Toad for mysql tool and deployed it and its working perfect without any problem. when I gave it to the admin to deploy in the production server they are getting errors.
Phpmyadmin 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
Mysql console error:
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 'END' at line 1
When asked, they are running the code from phpmyadmin and also they tried to run from mysql console they are getting errors
Its really frustrating and irritating why the same thing is running from a GUI tool and not working from a webtool or a console. 3 different behaviors in from three different tools
Then I tried the same thing and I got the error too. Its suprising me why
DROP TRIGGER IF EXISTS TRG_ ;
CREATE TRIGGER TRG_ BEFORE INSERT ON users FOR EACH ROW
BEGIN
DECLARE X INTEGER;
SELECT COUNT(*) into X FROM users;
IF X >= 16 THEN -- CHANGE THIS NUMBER
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'cant create more users';
END IF;
END;
Can some one tell me what I am doing wrong? I am not sure but I guess its something to do with delimiter keyword which I never understood the purpose.
Have a look at this question - MySQL: How do I use delimiters in triggers?
You should use delimiters when create source objects like triggers. The DELIMITER is not a MySQL statement, it a console command, and many MySQL clients supports this command. You may try this code in the MySQL console -
DROP TRIGGER IF EXISTS TRG_ ;
DELIMITER $$
CREATE TRIGGER TRG_ BEFORE INSERT ON users FOR EACH ROW
BEGIN
DECLARE X INTEGER;
SELECT COUNT(*) INTO X FROM users;
IF X >= 16 THEN -- CHANGE THIS NUMBER
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'cant create more users';
END IF;
END$$
DELIMITER ;
As I know some old phpmyadmin versions do not support delimiters.

MySQL CREATE TRIGGER, Syntax Error. What I'm doing wrong?

DELIMITER ||
CREATE TRIGGER `monthly_insert` BEFORE INSERT ON `history_monthly`
FOR EACH ROW
BEGIN
NEW.`uid` = CONCAT(OLD.`year`, OLD.`month`, OLD.`charactersId`);
END;
||
DELIMITER ;
And it returns an 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 '.`uid` = CONCAT(OLD.`year`, OLD.`month`, OLD.`charactersId`);
END' at line 4
It's my first time with triggers and I was doing my best to try to find soultion but I failed ;<
You need to add the word SET in this line:
SET NEW.`uid` = CONCAT(OLD.`year`, OLD.`month`, OLD.`charactersId`);
Also, as the commenter pointed out, there is no OLD value in a BEFORE INSERT trigger.