Stored procedure - One Line select statement Syntax ERROR

北慕城南 提交于 2020-04-18 00:49:16

问题


One line select statement throws syntax error in SP, but the query if executed doesn't produce any error.

The error changes to syntax error near - if I remove IN _HOSTID input parameter and DECLARE statements. Its very weird and I can't find the cause of the error.

DELIMITER //

CREATE PROCEDURE getTotalCCU(IN _HOSTID INTEGER)
BEGIN
DECLARE HOSTID INT;
DECLARE TOTAL_ITEM INT;

SELECT  item_count  FROM items WHERE hostid = '12345' and key_ LIKE '%_net%' AND NOT key_  LIKE '%Total_%' AND NOT key_ LIKE '%{#%' AND STATUS = '0';

END //

DELIMITER ;

Only when I try to import it, it throws syntax error on line 3.

mysql -uroot -p DB01 < getTotalNET.sql

ERROR 1064 (42000) at line 3: 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 12

回答1:


Figured out the issue.

Variable should not be the same as any column in the query.

DECLARE HOSTID INT;

SELECT  item_count  FROM items WHERE hostid = '12345' and key_ LIKE '%_net%' AND NOT key_  LIKE '%Total_%' AND NOT key_ LIKE '%{#%' AND STATUS = '0';

Since the query has a column named hostid we get a syntax error.

Solution rename the HOSTID variable to _HOSTID.

DECLARE _HOSTID INT;


来源:https://stackoverflow.com/questions/60952150/stored-procedure-one-line-select-statement-syntax-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!