问题
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