CREATE DEFINER=`test`@`%` PROCEDURE `f_m_dim_day`(in year VARCHAR(20))
begin
declare i int;
declare start_date varchar(20);
declare end_date varchar(20);
declare date_count int;
set i=0;
set start_date= concat(year, '-01-01');
set end_date = concat(year+1,'-01-01');
DELETE from m_dim_day where yr = year;
set date_count = datediff(end_date, start_date);
while i < date_count DO
INSERT into m_dim_day (dt,dt2,yr,mm,dd,dayofweek,weeknum,qr,bourse_week,yrmm,week_begin,week_end)
SELECT
DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y-%m-%d') dt,
DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y%m%d') dt2,
DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y') yr,
DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%m') mm,
DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%d') dd,
case DAYOFWEEK(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s')) when 1 then '星期日' when 2 then '星期一' when 3 then '星期二' when 4 then '星期三' when 5 then '星期四' when 6 then '星期五' when 7 then '星期六' end dayofweek,
DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%u') weeknum,
CONCAT(DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y'),quarter(STR_TO_DATE( start_date,'%Y-%m-%d %H:%i:%s'))) qr,
DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y-%u') bourse_week,
DATE_FORMAT(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),'%Y%m') yrmm,
subdate(date_format(start_date,'%Y-%m-%d'),if(date_format(date_format(start_date,'%Y-%m-%d'),'%w')=0,7,date_format(date_format(start_date,'%Y-%m-%d'),'%w'))-1) week_begin,
subdate(date_format(start_date,'%Y-%m-%d'),if(date_format(date_format(start_date,'%Y-%m-%d'),'%w')=0,7,date_format(date_format(start_date,'%Y-%m-%d'),'%w'))-7) week_end
from dual;
set i=i+1;
set start_date = DATE_FORMAT(date_add(STR_TO_DATE(start_date,'%Y-%m-%d %H:%i:%s'),interval 1 day),'%Y-%m-%d');
end while;
end