python - Looping through a string inside a Mysql Procedure -
python - Looping through a string inside a Mysql Procedure -
i have written store procedure takes comma separated string parameter. ex: id_list = '23, 45, 8, 106, 9, 33'. within procedure want loop through string, extract 1 id string @ time , pull info db. id used in status within procedure.
so ideally should id's this
23 45 8 106 9 33
i have tried doing this, extracting first id , stops.
delimiter ;; create procedure `procedure_test`(in id_list varchar(255)) begin declare id_length int default 0; declare id_sub_str int default 0; if id_list null set id_list = ''; end if; do_this: loop set id_length = length(id_list); select mechanics_name, mechanics_id `mechanics` employer_id = substring_index(id_list, ',', 1); set id_sub_str = char_length(substring_index(id_list, ',', 1)) + 2; set id_list = mid(id_list, id_sub_str, id_length); if id_list = '' leave do_this; end if; end loop do_this; end;; delimiter ;
could please suggest me how this. thanks
tested on machine
delimiter $$ create procedure procedurename(in param1 varchar(255)) begin set @text := trim(both ',' param1); set @strlen := 0; set @i := 1; while @strlen < length(@text) set @str := substring_index(@text, ',', @i); set @strlen := length(@str); set @i := @i + 1; insert t values( substring_index(@str, ',', -1)); -- print split value end while; end $$ delimiter ;
it insert info table t single column,so need create first.
create table t (blah int)
and phone call it
call procedurename('23, 45, 8, 106, 9, 33'); select * t 23 45 8 106 9 33
python mysql stored-procedures
Comments
Post a Comment