mysql - Call a stored procedure with an integer array as argument? -
i want pass integer array argument stored procedure, how can done?
create procedure rd22(unitlist int) begin select * abet inner join on a.id = abet.alarm_source , a.unit in (unitlist) abet.begin_timestamp = 1395874800000; end this how stored proc looks like, , want achive this:
set @val = [1,2,3]; call rd22(@val); one way can think pass string , somehow convert integers. there other subtle way achive this.
thanks time.
cheers.
you cant way, when pass param comma separated string in query take first value while executing in clause, need use dynamic query prepared statement
create procedure rd22(unitlist varchar(100)) begin set @ul = unitlist; set @qry = concat("select * abet inner join on a.id = abet.alarm_source , a.unit in(",@ul,") abet.begin_timestamp = 1395874800000"); prepare stmt @qry; execute stmt; end the call as
set @val = '1,2,3'; call rd22(@val); here test case in mysql
mysql> select * test ; +------+---------------------+---------------------+----------+ | id | created | resolved | status | +------+---------------------+---------------------+----------+ | 1 | 2015-05-10 00:00:00 | 1970-01-01 00:00:00 | open | | 2 | 2015-05-10 00:00:00 | 1970-01-01 00:00:00 | new | | 3 | 2015-05-10 00:00:00 | 2015-05-12 00:00:00 | resolved | | 4 | 2015-05-11 00:00:00 | 1970-01-01 00:00:00 | open | | 5 | 2015-05-11 00:00:00 | 1970-01-01 00:00:00 | new | | 6 | 2015-05-11 00:00:00 | 2015-05-11 00:00:00 | resolved | | 7 | 2015-05-12 00:00:00 | 1970-01-01 00:00:00 | open | | 8 | 2015-05-12 00:00:00 | 1970-01-01 00:00:00 | new | | 9 | 2015-05-12 00:00:00 | 1970-01-01 00:00:00 | open | +------+---------------------+---------------------+----------+ 9 rows in set (0.00 sec) mysql> delimiter // mysql> create procedure testin(vals varchar(100)) -> begin -> set @ul = vals; -> set @qry = concat("select * test id in(",@ul,")"); -> prepare stmt @qry; -> execute stmt; -> end;// query ok, 0 rows affected (0.03 sec) mysql> set @val = '3,4,6'; query ok, 0 rows affected (0.00 sec) mysql> call testin(@val); +------+---------------------+---------------------+----------+ | id | created | resolved | status | +------+---------------------+---------------------+----------+ | 3 | 2015-05-10 00:00:00 | 2015-05-12 00:00:00 | resolved | | 4 | 2015-05-11 00:00:00 | 1970-01-01 00:00:00 | open | | 6 | 2015-05-11 00:00:00 | 2015-05-11 00:00:00 | resolved | +------+---------------------+---------------------+----------+ 3 rows in set (0.01 sec)
Comments
Post a Comment