how to pass values with respective update table in mysql -
when call stored procedure passing in lists of data input:
in pk varchar(max) = “11;12;13” in forenam varchar(max)= “test1;test2;test3” in surnam varchar(max) = “tst1;tst2;tst3” in bdate varchar(max) = “2016-01-02; 2016-01-04; 2016-01-03” in comm varchar(max) = “comments1;comm2;comm3” in table_nam varchar(max)= “name1”
it should update table named “name1” below in 1 call sp
pk forename surname bdate comments 11 test1 tst1 2016-01-02 comments1 12 test2 tst2 2016-01-04 comm2 13 test3 tst3 2016-01-03 comm3` create definer=`username`@`%` procedure `sp_ukflmm_update_multitable`( in pk int, in forenam varchar(255), in surnam varchar(255), in bdate datetime, in comm varchar(1000), in table_nam varchar(255)) begin if table_nam in ('uk_flmm_demanddata','uk_flmm_completed_demanddata') update table_nam set forename=forenam,surname=surnam,birthdate=bdate,comments=comm id=pk; else if table_nam = 'uk_flmm_sequencedata' update table_nam set comments=comm id=pk; end if; end if; end$$ delimiter
i have tried above sp updating 1 row @ time.so can please me sort out issue.
try replacing below condition in clause
where locate(replace(id,';',','),pk) > 0;
hope should solve problem.
this update multiple records. update 'comments1;comm2;comm3'
3 ids.
if need update below
id comm 1 comments1 2 comm2 3 comm3
then not work.
in case can follow below sp
create definer=`username`@`%` procedure `sp_ukflmm_update_multitable`( in pk int, in forenam varchar(255), in surnam varchar(255), in bdate datetime, in comm varchar(1000), in table_nam varchar(255)) begin declare loopincrement int; declare looplimit int; declare npk int; declare nforename varchar(255); declare nsurname varchar(255); declare nbdate datetime; declare ncomm varchar(1000); set looplimit := 0; set loopincrement := 0; set looplimit := (length(pk) - length(replace(pk,";",""))) + 1; while loopincrement < looplimit if(loopincrement = looplimit - 1) set npk := substring_index(pk,';',-1); set nforename := substring_index(forenam,';',-1); set nsurname := substring_index(surnam,';',-1); set ncomm := substring_index(comm,';',-1); set nbdate := substring_index(bdate,';',-1); else set npk := substring_index(pk,';',loopincrement+1); set nforename := substring_index(forenam,';',loopincrement+1); set nsurname := substring_index(surnam,';',loopincrement+1); set ncomm := substring_index(comm,';',loopincrement+1); set nbdate := substring_index(bdate,';',loopincrement+1); end if; if table_nam in ('uk_flmm_demanddata','uk_flmm_completed_demanddata') update table_nam set forename=nforename,surname=nsurname,birthdate=nbdate,comments=ncomm id=npk; else if table_nam = 'uk_flmm_sequencedata' update table_nam set comments=ncomm id=npk; loopincrement++; end if; end if; end while; end$$ delimiter
note: sample sp implements scenario start with. improve if face issue.
Comments
Post a Comment