问一个关于游标的问题在线等 解决马上结帖子
declare rpt cursor for
select (select Sum(a.price * a.FConvert) as ProductSum from vw_StoreProductInDetail a where a.BillNumber = b.BillNumber) as ProductSum,
(select Sum(c.WareSum) as WareSum from WarehouseDetail c where c.DetailBill = b.BillNumber) as WareSum
from vw_StoreProductIn b where b.OperateTypeID= ' ' '10 ' ' '
declare @ProductSum decimal,
@WareSum decimal
open rpt
fetch next from rpt into @ProductSum,@WareSum
while @@fetch_status =0
Begin
--print @ProductSum
set @TopAllSum = @TopAllSum + @ProductSum - @WareSum
fetch next from rpt into @ProductSum,@WareSum
End
close rpt -- 结束后关闭游标
deallocate rpt
print @TopAllSum
这个游标返回的结果是空的 高手帮忙看看那里错了 ??? 游标不能这么用么 ???
------解决方案--------------------declare @ProductSum decimal,
@WareSum decimal
==>
declare @ProductSum decimal,
@WareSum decimal
set @ProductSum = 0
set @WareSum = 0
------解决方案-------------------- declare @ProductSum decimal,
@WareSum decimal,
@TopAllSum decimal
declare rpt cursor for
select (select Sum(a.price * a.FConvert) as ProductSum from vw_StoreProductInDetail a where a.BillNumber = b.BillNumber) as ProductSum,
(select Sum(c.WareSum) as WareSum from WarehouseDetail c where c.DetailBill = b.BillNumber) as WareSum
from vw_StoreProductIn b where b.OperateTypeID= ' ' '10 ' ' '
open rpt
fetch next from rpt into @ProductSum,@WareSum
while @@fetch_status =0
Begin
--print @ProductSum
set @TopAllSum = @TopAllSum + @ProductSum - @WareSum
fetch next from rpt into @ProductSum,@WareSum
End
close rpt -- 结束后关闭游标
deallocate rpt
print @TopAllSum
你这样再试试。不保证正确..语法现在是没错了。如果还不行的话。那就看看你查询那快查出的数据对不对先试着把@TopAllSum在begin..end之间输出看结果。然后把结果自己手动加起来看结果
------解决方案--------------------即是对的
@TopAllSum 输出也是最后一次的结果;前面的没掉了
这样有什么用?
是不是要在begin...end 里把结果插入一个变量表/临时表里存储起来呢?