2014-11-03 53 views
0

我有以下存储过程:MySQL的存储过程预处理语句不工作

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 

然后我运行它:

mysql> call getLastValueAutomaticShelter('current_level', 500)// 

并获得以下错误:

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 

任何想法?谢谢。

回答

2

您需要在里面添加一些空格:

SET @query = CONCAT('SELECT * FROM automatic_changes WHERE ',fieldName,' IS NOT NULL AND P_id=?'); 
/*             right^here....and^here*/ 

否则你最终的查询可能是这样的:

SELECT * FROM automatic_changes WHEREcolumnameIS NOT NULL AND P_id='whatever'; 

你的想法:)

+0

谢谢配合:) – MichalB 2014-11-03 09:38:24