日期:2014-05-19  浏览次数:20367 次

存储过程中循环
接受两个datatime型参数a,b
大概意思是这样:a <=b循环,如a=2007-1-1,b=2007-1-10就循环十次
存储过程中怎么写日期循环呢?大虾指点!十分感谢!互相学习!!

CREATE   PROCEDURE   __WebGetday(@opid   int,@errmessage   varchar(1000)   output,@datefrom   datetime,@dateto   datetime)   AS
--while     @datefrom <=@dateto
begin
select   totalcount=count(distinct   a.personid)   from   _human_person   a   where   (YEAR(CreateTime)   *   10000   +   MONTH(CreateTime)   *   100   +   DAY(CreateTime)   between   20040101   and   year(@datefrom)*10000+month(@datefrom)*100+day(@datefrom))

select   vipcount=count(distinct   a.personid)   from   _human_person   a   where   YEAR(CreateTime)   *   10000   +   MONTH(CreateTime)   *   100   +   DAY(CreateTime)   between   20040101   and   year(@datefrom)*10000+month(@datefrom)*100+day(@datefrom)   and   a.isfreereg   =0

select   freecount=count(distinct   a.personid)   from   _human_person   a   where   YEAR(CreateTime)   *   10000   +   MONTH(CreateTime)   *   100   +   DAY(CreateTime)   between   20040101   and   year(@datefrom)*10000+month(@datefrom)*100+day(@datefrom)   and   a.isfreereg   =1

select   newaddvipcount=count(distinct   b.personid)   from   _Human_Person   a   LEFT   OUTER   JOIN   _Trans_Index   b   ON   a.PersonId   =   b.PersonId   AND   b.IsRollback   =   0   AND   (b.PayMode   =   1   OR   b.PayMode   =   2   AND   b.PayInfo   <>   ' ')
WHERE   (a.isFreeReg   =   1)   and     YEAR(b.CreateTime)   *   10000   +   MONTH(b.CreateTime)   *   100   +   DAY(b.CreateTime)   between   year(@datefrom)*10000+month(@datefrom)*100+day(@datefrom)   and   year(@datefrom)*10000+month(@datefrom)*100+day(@datefrom)

select   newaddfreecount=count(distinct   a.personid)   from   _human_person   a   where   a.isfreereg   =   1   and   YEAR(CreateTime)   *   10000   +   MONTH(CreateTime)   *   100   +   DAY(CreateTime)   between   year(@datefrom)*10000+month(@datefrom)*100+day(@datefrom)   and   year(@datefrom)*10000+month(@datefrom)*100+day(@datefrom)

select   logincount=count(distinct   c.personid)   from   _human_person   a   left   join   _human_person_activedate   c
on   a.personid   =   c.personid   where   activedate=year(@datefrom)*10000+month(@datefrom)*100+day(@datefrom)

select   freelogincount=count(distinct   c.personid)   from   _human_person   a   left   join   _human_person_activedate   c
on   a.personid   =   c.personid   where   a.isfreereg   =   1   and   activedate=year(@datefrom)*10000+month(@datefrom)*100+day(@datefrom)

set   @datefrom=dateadd(d,1,@datefrom);
end
GO

------解决方案--------------------
while DATEDIFF(DAY,@datefrom,@datto)=0
------解决方案--------------------
测试看以下看能行不
哈哈