游标问题?请指教

mbjxj 2000-03-30 04:39:00
下面是一段SQL程序验证分析时提示如下错误请专家们解答,多谢!!
declare my_curr scroll cursor for select 收入, 支出, 余额 from 现金日报表
open my_curr
declare @remainder decimal(9)
declare @remainder1 decimal(9)
declare @income decimal(9)
declare @income1 decimal(9)
declare @out decimal(9)
declare @out1 decimal(9)
declare @count int
fetch first from my_curr into @income ,@out, @remainder1
SET @count=@@cursor_rows
while @count >=0
begin
fetch relative 0 from my_curr into @income1,@out1,@remainder1
fetch next from my_curr into @income ,@out, @remainder
set @remainder=@remainder1+@income-@out
update 现金日报表
set 余额 = @remainder
where current of my_curr
set @count=@count-1
select @remainder,@remainder1,@income,@out
end
close my_curr
deallocate my_curr

运行时错误:
Server: Msg 16930, Level 16, State 1, Line -1074283898
The requested row is not in the fetch buffer.
The statement has been terminated.

----------- ----------- ----------- -----------
3 3 345 345

(1 row(s) affected)

Server: Msg 16930, Level 16, State 1, Line 17
The requested row is not in the fetch buffer.
The statement has been terminated.

----------- ----------- ----------- -----------
3 3 345 345

(1 row(s) affected)


...全文
242 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanghuan 2000-04-07
  • 打赏
  • 举报
回复
我写误了一个地方,Count(a1.收入-a1.支出) 应该是 sum(a1.收入-a1.支出)
第一条也是起作用的,
tanghuan 2000-04-06
  • 打赏
  • 举报
回复
分析你的代码,你的程序可以不用游标,假设你的表中有一个关键字是按时间进行
如果没有,你可以用带自增长列的临时表序列化

表结构为: 现金日报表(收入, 支出, 余额,发生时间)

update 现金日报表 现金日报表.余额=a.余额
from 现金日报表 ,(
select Count(a1.收入-a1.支出) "余额",a2.发生时间
from 现金日报表 a1,现金日报表a2
where a1.发生时间<=a2.发生时间
group by a2.发生时间
) a
where 现金日报表.发生时间=a.发生时间
mbjxj 2000-04-06
  • 打赏
  • 举报
回复
好象可以,但是以上程序对记录中的第一条不起作用。
paul_feng 2000-04-06
  • 打赏
  • 举报
回复
1.不要用@COUNT做循环条件,改为用'WHILE @@FATCH_STATUS=0'
2.因为你用了SCROLL定义游标,默认的可更新选项无效,要在定义游标的最后加上FOR UPDATE选项来更新指定数据
3.还不行的话,把'WHERE CURRENT OF MY_CURR'改为
WHERE 收入=@INCOME AND 支出=@OUT AND 余额 =REMAINDER
mbjxj 2000-04-05
  • 打赏
  • 举报
回复
It's ok!!
thank you very much!
but it have another error:
Server: Msg 16930, Level 16, State 1, Line 17
The requested row is not in the fetch buffer.
The statement has been terminated.
haihong 2000-03-30
  • 打赏
  • 举报
回复
while @count >=0
该为while @count>=1试试
haihong 2000-03-30
  • 打赏
  • 举报
回复
while @count >=0
该为while @count>=1
tanghuan 2000-03-30
  • 打赏
  • 举报
回复
while @@fetch_status=0/*fetch_status : 0 successful 1: fail -2 :missing*/

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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