Mysql Prepared语句多次使用相同的参数

时间:2012-11-07 05:36:46

标签: mysql sql

我写了这样的存储过程:

  DELIMITER $$
   CREATE PROCEDURE searchByTerm(term VARCHAR(300))
    BEGIN
    SET @statment = "Select name,description from products where name like '%?%' OR description like '%?%'";
    PREPARE stmt FROM @statment;
    SET @a = term;
    SET @b = term;
    EXECUTE stmt USING @a,@b;
    DEALLOCATE PREPARE stmt; 
  END$$

将其称为:

CALL searchByTerm('xyz');

导致以下错误:

Error Code : 1210
Incorrect arguments to EXECUTE

我做错了什么?我知道它可以用concat声明完成,但为什么它不能像这样工作?我不能多次使用相同的参数?谢谢你的帮助..

1 个答案:

答案 0 :(得分:1)

在您的查询中使用 CONCAT

SET @statment = "Select name,description 
                 from products 
                 where name like CONCAT('%', ? ,'%') OR 
                       description like CONCAT('%', ? ,'%')";

所以您的完整查询将如下所示,

DELIMITER $$
CREATE PROCEDURE searchByTerm(term VARCHAR(300))
  BEGIN
    SET @statment = "Select name,description 
                     from products 
                     where name like CONCAT('%', ? ,'%') OR 
                           description like CONCAT('%', ? ,'%')";
    PREPARE stmt FROM @statment;
    SET @a = term;
    SET @b = term;
    EXECUTE stmt USING @a,@b;
    DEALLOCATE PREPARE stmt; 
  END$$