BEGIN
declare startdate,enddate date;
declare cnt1,leaveid bigint;
declare cnt int;
declare cur cursor
for
SELECT DISTINCT
  hhlp.leave_period_id
FROM hs_hr_leave_period hhlp WHERE hhlp.fy = FYY;
set cnt1=( SELECT DISTINCT
    COUNT(hhlp.leave_period_id)
  FROM hs_hr_leave_period hhlp WHERE hhlp.fy = FYY);
set cnt=1;
drop temporary table if exists tmp;
create temporary table tmp
(
Leave_Id bigint,
Leave_on date,
Leave_month int,
FY varchar(11)
);
open cur;
fetch next from cur into leaveid;
startloop:loop
if(cnt < cnt1)
then
set startdate=( SELECT
    hhlp.leave_period_start_date
  FROM hs_hr_leave_period hhlp
  WHERE hhlp.leave_period_id = leaveid);
set enddate=( SELECT
    hhlp.leave_period_end_date
  FROM hs_hr_leave_period hhlp
  WHERE hhlp.leave_period_id = leaveid);
while startdate <= enddate
do
if( dayofweek(startdate) <> 1 and dayofweek(startdate) <> 7 )
then
INSERT INTO tmp
  VALUES (leaveid, startdate, MONTH(startdate), FYY);
END IF;
set startdate=date_add(startdate,interval 1 day);
end while;
else 
leave startloop;
end if;
fetch next from cur into leaveid;
set cnt=cnt+1;
end loop;
close cur;
SELECT
 Leave_Id,
Leave_on,
Leave_month,
FY,
  month(Leave_on)
FROM tmp; 

END