【问题标题】:MySQL Stored Procedure with Prepared Statement does not work带有准备好的语句的 MySQL 存储过程不起作用
【发布时间】:2014-11-03 08:37:09
【问题描述】:

我有以下存储过程:

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

有什么想法吗?谢谢。

【问题讨论】:

    标签: mysql stored-procedures prepared-statement


    【解决方案1】:

    您需要在其中添加一些空格:

    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';
    

    你明白了:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 2015-10-11
      • 1970-01-01
      相关资源
      最近更新 更多