2012-11-07 142 views
2

Mysql的預處理語句我寫存儲過程是這樣的:使用相同的參數多次

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

使用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$$ 
+0

偉大......這worked..many感謝: )我不認爲''真的是一個愚蠢的錯誤'?'作爲字符串的一部分 – Eez

+0

@eez歡迎:D –

相關問題