Having trouble doing multiple cursors in a MySQL stored procedure -
i'm writing store procedure create 2 temporary tables union select of two. when using either first or second cursor alone other commented procedure works, when run query create procedure 2 cursors, fails.i've changed code reflect ike walker's suggestion.
here script:
delimiter // drop procedure if exists joinemailsmsdailygraph// create procedure joinemailsmsdailygraph(in previousday varchar(20), in today varchar(20)) reads sql data begin declare hours int; declare sms int; declare email int; declare smsdone int default 0; declare emaildone int default 0; declare cursorsms cursor select hour(sm.date_created) `hour of day`, count(*) smscount sms_message_delivery smd join sms_message sm on sm.sms_message_id = smd.sms_message_id date(sm.date_created) >= date(previousday) , date(sm.date_created) < date(today) group hour(sm.date_created); declare continue handler not found set smsdone =1; declare cursoremail cursor select hour(em.date_created) `hour of day`, count(*) emailcount email_message_delivery emd left join email_message em on emd.email_message_id=em.email_message_id date(em.date_created) >= date(previousday) , date(em.date_created) < date(today) group hour(em.date_created); declare continue handler not found set emaildone =1; drop temporary table if exists tempsms; create temporary table tempsms (hours_day int, sms_count int, email_count int); open cursorsms; sms_loop: loop fetch cursorsms hours , sms; if smsdone = 1 leave sms_loop; end if; insert tempsms (hours_day, sms_count) values (hours, sms); end loop sms_loop; close cursorsms; drop temporary table if exists tempemail; create temporary table tempemail (hours_day int , sms_count int , email_count int); open cursoremail; email_loop: loop fetch cursoremail hours, email; if emaildone=1 leave email_loop; end if; insert tempemail(hours_day, email_count) values(hours, email); end loop email_loop; close cursoremail; select hours_day, sms_count , email_count tempsms union select hours_day, sms_count, email_count tempemail; end// delimiter;
it gives error
query : create procedure joinemailsmsdailygraph(in previousday varchar(20), in today varchar(20)) reads sql data begin declare hours int...
error code : 1338
cursor declaration after handler declaration
execution time : 00:00:00:000
transfer time : 00:00:00:000
total time : 00:00:00:000
ive tried putting both continue handlers @ end of declare section complains declare block overlapping or so.
can please tell me i'm doing wrong? reading.
why using cursors ? without tmp table using union.
drop procedure if exists join_email_sms_daily_graph; delimiter # create procedure join_email_sms_daily_graph ( in previousday varchar(20), in today varchar(20) ) begin create temporary table tmp ( hours_day int unsigned, sms_count int unsigned default 0, email_count int unsigned default 0 )engine=memory; insert tmp (hours_day, sms_count) select hour(sm.date_created) hours_day, count(*) sms_count sms_message_delivery smd join sms_message sm on sm.sms_message_id = smd.sms_message_id date(sm.date_created) >= date(previousday) , date(sm.date_created) < date(today) group hour(sm.date_created); insert tmp (hours_day, email_count) select hour(em.date_created) hours_day, count(*) email_count email_message_delivery emd left join email_message em on emd.email_message_id=em.email_message_id date(em.date_created) >= date(previousday) , date(em.date_created) < date(today) group hour(em.date_created); select * tmp; drop temporary table if exists tmp; end# delimiter;
Comments
Post a Comment