最近在SQL Server的项目中需要做一个统计谁来了,谁没来的查询,所以需要得到一个某段时间内日期完整的列表再和数据库中已经有的时间进行左连接.
思考如下:首先我需要一个循环,于是就需要一个循环控制,但什么表是每个数据库中都有的呢,这时我就想到系统表中sysobjects,
于是拿它做了循环体,结构比较简单,也许有朋友会需要.
代码如下:
declare @StartDate nvarchar(10)
declare @EndDate nvarchar(10)
set @EndDate = '2008-02-28'
set @StartDate = '2008-02-01'
Select year(dateadd(day,id,@StartDate)) as yyear ,month(dateadd(day,id,@StartDate)) as mmonth ,
day(dateadd(day,id,@StartDate)) as dday ,DATEPART(dw,dateadd(day,id,@StartDate)) as wweekday
from
(Select top 50
(select count(*) from sysobjects where id<= a.id)-1 as id from sysobjects a) aa
where dateadd(day,id,@StartDate ) <= @EndDate
思考如下:首先我需要一个循环,于是就需要一个循环控制,但什么表是每个数据库中都有的呢,这时我就想到系统表中sysobjects,
于是拿它做了循环体,结构比较简单,也许有朋友会需要.
代码如下:
declare @StartDate nvarchar(10)
declare @EndDate nvarchar(10)
set @EndDate = '2008-02-28'
set @StartDate = '2008-02-01'
Select year(dateadd(day,id,@StartDate)) as yyear ,month(dateadd(day,id,@StartDate)) as mmonth ,
day(dateadd(day,id,@StartDate)) as dday ,DATEPART(dw,dateadd(day,id,@StartDate)) as wweekday
from
(Select top 50
(select count(*) from sysobjects where id<= a.id)-1 as id from sysobjects a) aa
where dateadd(day,id,@StartDate ) <= @EndDate