Search code examples
mysqlsqlselectin-clausefind-in-set

IN Clause dont work in MySQL for me


I am passing my parameter as 'Suburbun','Indigo' to retrieve records matching both Campaigns in below Stored Procedure created in MySql.

CREATE PROCEDURE `DemoSP`(Campaign VARCHAR(3000))
BEGIN
    SET @query = CONCAT('Select * from vicidial_log WHERE campaign_id IN (?)');
  PREPARE stmt FROM @query;
  SET @CampaignID = Campaign;
  EXECUTE stmt USING @CampaignID;
  DEALLOCATE PREPARE stmt;
END;

It Doesn't give any rows!

But when i pass only 'Suburbun' in SP, it gives 6 Rows!

Where am i going wrong?

--Answer !

I tried as Lee Fentress commented in http://www.poolofthought.com/index.php/2008/12/28/a-comma-seperated-list-as-parameter-to-mysql-stored-procedure/ and peterm answer reflected similar coding,

It worked!

Thanks, but i find this negative mark as compared to SQL Server.

Gee, Thank you Guys!!


Solution

  • You won't be able to use USING in this case. You can just build the full query sting and execute it without parameters

    DELIMITER $$
    CREATE PROCEDURE DemoSP(Campaign VARCHAR(3000))
    BEGIN
      SET @query = CONCAT('SELECT * FROM vicidial_log WHERE campaign_id IN (', Campaign, ')');
      PREPARE stmt FROM @query;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;
    END$$
    DELIMITER ;
    

    Note: make sure that delimited values that you pass in Campaign are properly quoted (like you said they are) and quotes in values, if there is any, are escaped.

    Here is SQLFiddle demo