2

我写了这样的存储过程:

  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 语句来完成,但为什么它不能这样工作?我不能多次使用相同的参数吗?谢谢你的帮助..

4

1 回答 1

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$$
于 2012-11-07T05:39:31.067 回答