分配准备好的查询错误语法错误

时间:2017-09-21 09:39:49

标签: mysql prepared-statement

我尝试在自定义mysql函数中执行变量查询,这是脚本:

DELIMITER $

CREATE FUNCTION is_present(in_id BIGINT, in_table_name VARCHAR(255)) RETURNS BIT
BEGIN
  DECLARE stm VARCHAR(255);
  DECLARE result BIT DEFAULT 0;

  SET stm := CONCAT('SELECT IF(COUNT(*), 1, 0) INTO result FROM', in_table_name, 'WHERE id=? LIMIT 1');
  PREPARE query FROM stm;
  EXECUTE query USING in_id;
  DEALLOCATE PREPARE query;

  RETURN result;
END $

DELIMITER ;

Mysql警告我语法:

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以获得正确的语法   靠近'stm;执行查询USING in_id; DEALLOCATE PREPARE查询;     返回结果; '第9行

1 个答案:

答案 0 :(得分:0)

我认为你在var name之前缺少一个分号:

PREPARE query FROM :stm;