Mysql – Stored procedure that accepts an id as a parameter – how to execute for multiple ids list

MySQLstored-procedures

Hi I have the following stored procedure that works for a single id (field22):

CREATE DEFINER=`root`@`localhost` PROCEDURE `update_procedure`(IN f22  INT)
BEGIN

UPDATE Table1 p join Table2 b set p.f1= (p.f1-  (LEAST(p.f2,b.amount)*coalesce((select rate from CurrencyExchangeRate where   fromCurrency=b.currency and toCurrency=p.currency and date(date)='2017-01-  15'),1))) ,
 p.f5= (p.f5- (LEAST(p.f2,b.amount)*coalesce((select f6 from   CurrencyExchangeRate where fromCurrency=b.currency and toCurrency=p.currency and   date(date)='2017-01-15'),1))),
p.f7= (p.f7- (LEAST(p.f8,b.amount)*coalesce((select rate from    CurrencyExchangeRate where fromCurrency=b.currency and toCurrency=p.currency and date(date)='2017-01-15'),1))) where status!=2 and p.id=b.f13 and b.id=f22;


UPDATE Tabl2 SET status=2, f1=3,changeDate=now() where id=f22 and status!=2;


INSERT INTO Table4 (type,f1,f2,date,f3,amount,f5,f6) select 11,2,tr.id,now(),tx.f11,tr.f12,tr.amount-tr.f13+tr.f14,tx.id from Table6 tr join  Table7 tx where tr.id=tx.f77 and tr.id=f22;

END

I would like to execute this stored procedure for a known list of ids i have:

  [1,22,443, 2112 ...];

Whats the quickest way to go about it?

Best Answer

If you can pass in the string `"1,22,443", then you construct the query:

SET @sql := CONCAT("... IN (", f22, ")...");

then PREPARE it and EXECUTE.

Not clean, but it should work.

If there is only one element to send in, the IN will still work fine. (An empty string would lead to a syntax error.)