日期:2014-05-17  浏览次数:20543 次

通过SQL来实现日期明细
我有一个表,存着员工和请假时间,数据表格式如下
name    holiday_begin_date     holiday_end_date
  A         2013-01-16        2013-02-16
  b         2013-02-05        2013-02-05
  c         2013-02-15        2013-02-17
  ..        ............       ...........(以下数据省略)

假设,现在是2月份
现在想通过SQL来判断,2月的1~28号,所有员工的休假明细
即想通过当月的日期明细来left join 休假明细.

目前休假明细实现不了,请指教。谢谢

------解决方案--------------------


Create function [dbo].[generateTime]
 
(
 
    @begin_date datetime,
 
    @end_date datetime
 
)

returns @t table(date datetime)
 
as
 
begin
 
    with maco as
 
    (
 
       select @begin_date AS date
 
       union all
 
       select maco.date+1  as date from maco
 
       where date+1 <=@end_date
 
    )
 
    insert into @t
 
    select * from maco option(maxrecursion 0);
 
    return
 
end

go
select * from dbo.generateTime('2013-2-1','2013-2-28')