sql while 循环 跪求帮忙 谢谢了
declare @DATE datetime
set @DATE = :SDATE
while @DATE <= :EDATE
begin
declare @OILPUMPSUM money
select @OILPUMPSUM = sum(isnull(DSALEQTY*OLDPRICE,0))
from OIL_PUMPPRICECHG
where RUNDATE=@DATE
declare @OILTMPSUM money
select @OILTMPSUM = sum(T.SALEAMOUNT)
from TURNPAYSUM T, GOODS G
where T.RUNDATE=@DATE
and T.GOODSID = G.GOODSID
and G.ASSCODE IN (9,10)
and T.PAYCODE <> '1'
set @DATE = @DATE + 1
end
这段sql会报Operand type clash: INT is incompatible with DATETIME 的错 应该是 set @DATE = :SDATE 的问题但是不知道原因 不知道怎么解决 求大神帮帮忙
------解决方案--------------------
SQL code
set @DATE = dateadd(dd,1,@DATE)--如果是加一天
set @DATE = dateadd(mm,1,@DATE)--加一月
set @DATE = dateadd(hh,1,@DATE)--加一小时
--日期不能直接加减,需要用dateadd(增加部分,增加量,被增加的日期的初始值)