Can anyone tell me why I am getting this error in MySql:
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 'NULL' at line 1
Here is my code:
CREATE PROCEDURE `msgBoardGetComments2`(
IN _StoryID INT,
IN _RowNum INT
)
BEGIN
DECLARE _sql VARCHAR(1000);
SET _sql := CONCAT('SELECT c.CommentText, c.CommentDate, a.UserName
FROM comments c
LEFT JOIN accounts a
ON c.UserID = a.UserID
LEFT JOIN stories s
ON c.StoryID = s.StoryID
WHERE c.StoryID = ',_StoryID, '
ORDER BY c.CommentDate DESC
LIMIT 10
OFFSET ', _RowNum);
PREPARE stmt1 FROM #_sql;
EXECUTE stmt1;
END
I believe you may be receiving that error when _RowNum is NULL. If this is the case, you can try adding an IF condition to optionally include the OFFSET _RowNum if the _RowNum is NULL. You can test this by printing your query before execution:
SELECT _sql;
--PREPARE stmt1 FROM #_sql;
--EXECUTE stmt1;
Also, I haven't seen the use of an underscore (_) for variable assignment, perhaps that may also be playing a role in this?
Related
is there any way to use if-else statement in mysql outside sps and functions in ad hoc queries? It can be easily done is MS SQL.
This is my query:
SET #Flag := 'u', #appUserID := 0;
SELECT app_user_id
INTO #appUserID
FROM app_user
WHERE user_login_id = #UserLoginID;
IF #Flag = 'u' THEN
SELECT #appUserID;
END IF;
I receive this 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 'IF #Flag = 'u' THEN SELECT #appUserID' at line 1"
Running this in a normal query tab, you can use DDL
SET #Flag := 'u', #appUserID := 0;
SELECT app_user_id
INTO #appUserID
FROM app_user
WHERE user_login_id = #UserLoginID;
SELECT IF(#Flag = 'u',#sql := "SELECT #appUserID; ",#sql := "DO NULL");
PREPARE stmt1 FROM #sql;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
I have tried various modifications to make this work but I can't seem to remove the error. My SQLQuery string is much larger than the one below but same set up. I tried using a CONCAT(' ') statement but had issues. So I tried using this format but still receive the same error. any suggestions?
CREATE DEFINER=`root`#`%` PROCEDURE `stp_Select_GetTbl`(IN strWhereClause
VARCHAR(255))
BEGIN
DECLARE SQLQuery varchar(1000);
SET SQLQuery = '
SELECT `Part Num` AS `PPNumber`,
`Shop Num` as `SOrder`
FROM `track`.`s list` WHERE ';
SET #SQLQuery = #SQLQuery + strWhereClause + '';
PREPARE stmt FROM #SQLQuery;
EXECUTE stmt;
END
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 '0' at line 1
`
I have tried not declaring SQLQuery... changing varchar size... reorganizing and even using the example below
SET #getList =
CONCAT('SELECT (t1, t2, t3) FROM (SELECT t1, t2, t3) as mainSelect WHERE ', strWhereClause, '');
PREPARE stmt FROM #getList;
EXECUTE stmt;
END
but get error at
.... MySQL server version for the right syntax to use near '0
I need to define a string for my SELECT statement because I create the WHERE clause in my stored procedure.
I am trying get count value from a table and storing it in a variable using select statement in stored procedure. But when I use following lines of statemnts
DECLARE totalRegister INTEGER;
SET #sqlstmt = CONCAT('SELECT COUNT(DISTINCT(msisdn)) INTO ', totalRegister, ' FROM sm_history.svc_mgmt_', #yesterdayMonth,' WHERE action_type = 1 AND DATE(created) = ', yesterday);
PREPARE statement FROM #sqlstmt;
EXECUTE statement;
DEALLOCATE PREPARE statement;
I get following error on calling the stored procedure
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 'NULL' at line 1
I further debugged my stored procedure by using Select #sql; and surprised to see it was returning NULL
Edit 1: Variables yesterday and yesterdayDate are returning values correctly so I am sure there is no issue with these variables. The problem is due to totalRegister variable
Luckily I got solution by using hit and trial. Instead of using totalRegister as variable in the CONCAT function replaced it with #totalRegister which caused the stored procedure to work fine.
DECLARE totalRegister INTEGER;
SET #sqlstmt = CONCAT('SELECT COUNT(DISTINCT(msisdn)) INTO #totalRegister FROM sm_history.svc_mgmt_',#yesterdayMonth ,' WHERE action_type = 1 AND DATE(created) = ', "'", yesterday, "'");
PREPARE statement FROM #sqlstmt;
EXECUTE statement;
DEALLOCATE PREPARE statement;
select #totalRegister;
Above thing worked fine. Anyways thanks to all for contribution
I have the following stored procedure:
CREATE PROCEDURE getLastValueAutomaticShelter(IN fieldName varchar(30), position_number INT)
BEGIN
SET #query = CONCAT('SELECT * FROM automatic_changes WHERE',fieldName,'IS NOT NULL AND P_id=?');
PREPARE stmt FROM #query;
SET #position_number=position_number;
EXECUTE stmt USING #position_number;
DEALLOCATE PREPARE stmt;
END
Then I am running it:
mysql> call getLastValueAutomaticShelter('current_level', 500)//
And getting the following 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 'NOT N
ULL AND P_id=?' at line 1
Any ideas? Thanks.
You need to add some spaces in there:
SET #query = CONCAT('SELECT * FROM automatic_changes WHERE ',fieldName,' IS NOT NULL AND P_id=?');
/* right ^ here....and ^ here*/
Otherwise your final query might look like this:
SELECT * FROM automatic_changes WHEREcolumnameIS NOT NULL AND P_id='whatever';
You get the idea :)
Please can anyone tell me why this stored procedure is not working , what am I doing wrong.It keeps giving syntax error.
puserid and plimit are the parameters passed in the procedure.
DECLARE uid BIGINT;
DECLARE rangee TINYINT;
SET #uid = puserid;
SET #rangee = plimit * 15;
PREPARE STMT FROM
'IF((SELECT count(type) from notificationrecievers nr where
nr.status=0 and nr.recieverid=?) = 0) THEN
select nr.type,n.senderid,n.postid,u.name,nr.status
from
user u,notifications n,notificationrecievers nr where
nr.recieverid=? and u.userid=n.senderid and
nr.notificationid=n.notid order by n.notid desc
limit 15 OFFSET ?;
ELSE
select nr.type,n.senderid,n.postid,u.name,nr.status
from
user u,notifications n,notificationrecievers nr where
nr.recieverid=? and u.userid=n.senderid and nr.status=0 and
nr.notificationid=n.notid order by n.notid desc;
END IF;';
EXECUTE STMT USING #uid,#uid,#rangee,#uid;
Here's the error that i get.
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 'IF((SELECT count(type) from notificationrecievers nr where nr.status=0 and nr.r' at line 1
You can use only DML in prepared statements.
You can do something like this
IF condition THEN
set #q = 'your query text'
ELSE
set #q = 'another query'
END IF;
PREPARE stmt FROM #q
EXECUTE stmt using ...
DEALLOCATE PREPARE stmt
You can execute some queries before to evaluate your conditions.
Hope it helps