MYSQL生成日历表,通常在做报表的时候需要用来生成一个临时表,用来左连接等。...

最近在做一个报表,数据库是用MYSQL,于是需要个日历表,

通常在做报表的时候会经常使用到。

 

ExpandedBlockStart.gif 代码
CREATE  DEFINER  =   ' root ' @ ' localhost '   PROCEDURE  `create_tmp_yearmonth_tb`()
    
NOT  DETERMINISTIC
    
CONTAINS  SQL
    SQL SECURITY DEFINER
    COMMENT 
''
BEGIN
  #变量声明    
  
declare  sourcedate date;
  
declare  targetdate date;
  
declare  indexdate date;
  
declare  index_month  int ;
  
declare  step_year_month  char ( 20 );
  
  
set  sourcedate  =  date( ' 2010-01-01 ' );
  
set  targetdate  =  date( ' 2010-05-01 ' );
  
set  indexdate  =  sourcedate;
    
  
set  index_month  =   month (indexdate);
    #执行循环
   
while  indexdate  <=  targetdate do  
          
begin
             
set  index_month  =   month (indexdate);
             
set  step_year_month  =   concat( cast ( year (indexdate)  as   char ) , ' - ' , cast (index_month  as   char ), ' -01 ' );
             
insert  tmp_ym_tb(ym)  values (step_year_month); 
             
set  step_year_month  =   concat( cast ( year (indexdate)  as   char ) , ' - ' , cast ((index_month + 1 as   char ), ' -01 ' );
             
set  indexdate  =  date(step_year_month);
        
end ;
   
end   while ;


END ;

转载于:https://www.cnblogs.com/upshania/archive/2010/12/29/1920935.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值