Domanda

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

È stato utile?

Soluzione

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

Altri suggerimenti

Try this:

There is no need to use PREPARE STATEMENT. You can get the result using FIND_IN_SET() function

SELECT * FROM vicidial_log WHERE FIND_IN_SET(campaign_id, Campaign)

try this

"Select * from vicidial_log WHERE campaign_id IN ('?')"

instead of

'Select * from vicidial_log WHERE campaign_id IN (?)'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top