代码改变世界

sqlserver 时间递增循环

2012-05-09 10:46  java ee spring  阅读(2407)  评论(0编辑  收藏  举报

 

--查询指定供应商指定的一段时间内出票的张数

如果每查询一个月,修改一次时间太麻烦,写个循环的!

declare @date1 date
declare @date2 date
declare @startdate date
declare @enddate date
declare @countsum int
declare @count int
set @startdate='2010-06-01'
set @enddate='2011-07-01'
set @count=1
set @countsum=DATEDIFF(MM,@startdate,@enddate)
set @date1=@startdate
set @date2=dateadd(mm,1,@startdate)
while @count<@countsum
begin
--执行sql
SELECT SUM(PassengerCount) AS 张数 FROM dbo.Orders WHERE ProviderID =
(SELECT UserId FROM dbo.User
 WHERE Username='wind2006')
and OrderTime Between @date1 and @date2

 

set @date1=dateadd(mm,1,@date1)
set @date2=dateadd(mm,1,@date2)
set @count=@count+1
end