Aleks Ben Maza
Aleks Ben Maza

Reputation: 327

allocated prepared query error syntax error

I try to execute a variable query within a custom mysql function, here's the script :

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 warns me about the syntax :

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 'stm; EXECUTE query USING in_id; DEALLOCATE PREPARE query; RETURN result; ' at line 9

Upvotes: 0

Views: 25

Answers (1)

divix
divix

Reputation: 1374

I think you are missing a semicolon before var name:

PREPARE query FROM :stm;

Upvotes: 0

Related Questions