问一个关于游标的问题在线等 解决马上结帖子

dcren118 2007-03-08 04:51:59
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
这个游标返回的结果是空的 高手帮忙看看那里错了 ??? 游标不能这么用么 ???
...全文
169 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sbqcel 2007-03-08
  • 打赏
  • 举报
回复
即是对的
@TopAllSum 输出也是最后一次的结果;前面的没掉了
这样有什么用?
是不是要在begin...end 里把结果插入一个变量表/临时表里存储起来呢?
KIEKI-LEO 2007-03-08
  • 打赏
  • 举报
回复
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之间输出看结果。然后把结果自己手动加起来看结果
cpp2017 2007-03-08
  • 打赏
  • 举报
回复
TopAllSum 也要初始化
dcren118 2007-03-08
  • 打赏
  • 举报
回复
那有什么方法能解决这里累加的问题么 ?? 谢谢 !
KIEKI-LEO 2007-03-08
  • 打赏
  • 举报
回复
你的这个变量'@TopAllSum'放在begin...end中能不能输出值??
我在怀疑是你变量作用域的问题哦
dcren118 2007-03-08
  • 打赏
  • 举报
回复
还是不行呢 !

我这里面的两个值是 上面 SELECT 语句里面的
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'''
这里面的返回的值 然后累加付给 @TopAllSum 最后在用@TopAllSum干其他事情 !
cpp2017 2007-03-08
  • 打赏
  • 举报
回复
declare @ProductSum decimal, @WareSum decimal ==> declare @ProductSum decimal, @WareSum decimal set @ProductSum = 0 set @WareSum = 0
wshuangminlg 2007-03-08
  • 打赏
  • 举报
回复
帮LZ

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧