2

I wrote stored procedure like this:

  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$$

calling it as :

CALL searchByTerm('xyz');

it results in following error:

Error Code : 1210
Incorrect arguments to EXECUTE

Am i doing anythng wrong? I know it can be done with concat statement but why it's not working like this? Can't i use same parameter multiple times? Thanks for any help..

1 Answer 1

1

use CONCAT in your query

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

so your full query will look like this,

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$$
Sign up to request clarification or add additional context in comments.

1 Comment

great... it worked..many thanks :) it was really a silly mistake that i didn't consider '?' as part of string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.