mysql stored procedure, traverse table data with cursor

BEGIN

#Routine body goes here...

DECLARE nidCode varchar(50);

DECLARE m smallint(2);

DECLARE e smallint(2);

DECLARE p smallint(2);

DECLARE j smallint(6);

DECLARE x smallint(6);

DECLARE jpushpage varchar(32);

 

#channel

DECLARE channels VARCHAR(20) default '';

DECLARE smsC VARCHAR(20)  default 'sms';

DECLARE smsCD VARCHAR(20)  default ',sms';

DECLARE emailC VARCHAR(20)  default 'email';

DECLARE emailCD VARCHAR(20)  default ',email';

 

DECLARE sysC VARCHAR(20)  default 'sys';

DECLARE sysD VARCHAR(20)  default ',sys';

 

DECLARE jpushC VARCHAR(20)  default 'jpush';

DECLARE jpushCD VARCHAR(20)  default ',jpush';

 

DECLARE xgC VARCHAR(20) default 'xg';

DECLARE xgCD VARCHAR(20) default ',xg';

 

Is there a record of nid in #t_message_template

DECLARE cnt int default 0;

#Modified several records of message.t_message_template in total

DECLARE modifyCount int default 0;

 

#This is used to handle the case where the cursor reaches the last row  

DECLARE s int default 0;

 

#declare cursor cursor_name (cursor_name is a multi-row result set)  

DECLARE cursor_name CURSOR FOR select nid,message,email,phone,jpush,xg,jpush_page from ucdai.yyd_remind;  

 

#Set a stop marker   

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET s=1;  

 

#Open the cursor  

OPEN cursor_name; 

 

#Get the record of the current pointer of the cursor, read a row of data and pass it to the variables a, b 

fetch  cursor_name into nidCode,m,e,p,j,x,jpushpage ;  

set modifyCount = 0;

 

#Start the loop to determine whether the cursor has reached the end as the loop condition   

while s <> 1 do  

 

            

set channels = '';

#system information

if m =1 || m=3 THEN

if channels = '' THEN

set channels = concat(channels,sysC);

else

set channels = concat(channels,sysCD);

end if;

end if;

#email

if e=1 || e=3 THEN

if channels = '' THEN

set channels = concat(channels,emailC);

else

set channels = concat(channels,emailCD);

end if;

end if;

#sms

if p=1 || p=3 THEN

if channels = '' THEN

set channels = concat(channels,smsC);

else

set channels = concat(channels,smsCD);

end if;

end if;

#jpush

if j=1 || j=3 THEN

if channels = '' THEN

set channels = concat(channels,jpushC);

else

set channels = concat(channels,jpushCD);

end if;

end if;

#xg

if x=1 || x=3 THEN

if channels = '' THEN

set channels = concat(channels,xgC);

else

set channels = concat(channels,xgCD);

end if;

end if;

 

#set nidCode = '41';

select count(*) into cnt from t_message_template where nid = nidCode;

#SELECT cnt;

if cnt > 0 THEN

update t_message_template set channel = channels,jpush_page= jpushpage,UPDATE_TIME =SYSDATE() where nid= nidCode;

set modifyCount = modifyCount + cnt;

end if;

 

#read the next line of data  

           fetch  cursor_name into nidCode,m,e,p,j,x,jpushpage;  

            

  end while;  

                   

  #Close the cursor  

  CLOSE cursor_name ;  

select modifyCount;

   

END

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326814711&siteId=291194637