请帮忙看看这个游标处理,是否存在问题。

wea1978 2005-03-10 02:04:41
declare @s varchar(4000),@gs int,@jb int,@j_int int

--定义游标
set @s='declare mycursor cursor for
select 补时,补加班,工时电脑工号 from '+ @FILENAMEGS + ' WHERE (工时电脑工号 in(select 工时电脑工号 from temp_selfind)
AND (工时日期 like '+ @s_date +'))'
exec(@s)
open mycursor -- 打开游标
fetch next from mycursor into @gs,@jb,@j_int
while @@fetch_status=0
begin
If @Weekday = 0
begin
exec('
update '+ @FILENAMEGSY +' set 月补时 = 月补时 -'+ @gs +',
月补加班= 月补加班 -'+ @jb +',
月休假时间 = 月休假时间 -'+ @gs +'-'+ @jb +'
where (月工时电脑工号 ='+ @j_int +')
AND (月工时日期 like left('+ @s_date +',6))
update '+ @FILENAMEGSY +' set 月补时 = 月补时 + '+ @s_all +',
月补加班= 月补加班 + '+ @s_addh +',
月休假时间 = 月休假时间 +'+ @s_addh +'+ '+ @s_all +'
where (月工时电脑工号 ='+ @j_int +')
AND (月工时日期 like left('+ @s_date +',6))
update '+ @FILENAMEGS +' set 补时 = '+ @s_all +',补加班= '+ @s_addh +'
where (工时电脑工号 ='+ @j_int +') AND (工时日期 like '+ @s_date +')')
end
Else
begin

exec('update '+ @FILENAMEGS +' set 补时 = '+ @s_all +',补加班='+ @s_addh +'
where (工时电脑工号 ='+ @j_int +') AND (工时日期 like '+ @s_date +')
update '+ @FILENAMEGSY +'
set 月补时 = 月补时 - '+ @gs +'+ '+ @s_all +',
月补加班= 月补加班 - '+ @jb +'+'+ @s_addh +'
where (月工时电脑工号 ='+ @j_int +') AND
(月工时日期 like left('+ @s_date +',6)) ')
end
end
Fetch next from mycursor Into @gs,@jb,@j_int --没有继续读取下一条

close mycursor
deallocate mycursor
---------处理这个游标会不会失败呢?没运行时数据是更新了,但好象没退出来。
...全文
111 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yown 2005-03-10
  • 打赏
  • 举报
回复
Fetch next from mycursor Into @gs,@jb,@j_int --没有继续读取下一条

这一条应该放在

while ...
begin
......


Fetch next from mycursor Into @gs,@jb,@j_int --放这里
end
wea1978 2005-03-10
  • 打赏
  • 举报
回复
咦?我不是结贴了吗?怎么没看到分数呢?
wea1978 2005-03-10
  • 打赏
  • 举报
回复
谢谢 zjcxc(邹建) 大侠,第一次写游标,感觉很不好懂。
Xmafu123 2005-03-10
  • 打赏
  • 举报
回复
yun,,,,,,
kuang yun......
yun dao !!!!!!!
zjcxc 元老 2005-03-10
  • 打赏
  • 举报
回复
问题还是比较多,均用--**==========标明了
zjcxc 元老 2005-03-10
  • 打赏
  • 举报
回复
--declare @s varchar(4000),@gs int,@jb int,@j_int int
declare @s varchar(4000),@gs varchar(10),@jb varchar(10),@j_int varchar(10)
--**==========改 @gs,@jb,@j_int 定义,避免不能直接用在字符串+运算中

--定义游标
set @s='declare mycursor cursor global --**==========最好显示定义为全局部游,避免受数据库选项设置的影响
for
select 补时,补加班,工时电脑工号 from '+ @FILENAMEGS --**==========这个变量没有定义
+ ' WHERE (工时电脑工号 in(select 工时电脑工号 from temp_selfind)
AND (工时日期 like '+ @s_date +'))' --**==========这个变量没有定义
exec(@s)
open mycursor -- 打开游标
fetch next from mycursor into @gs,@jb,@j_int
while @@fetch_status=0
begin
If @Weekday = 0
begin
--**========== @gs,@jb,@j_int 均定义为int类型,不能直接参与字符串运算
exec('
update '+ @FILENAMEGSY +' set 月补时 = 月补时 -'+ @gs +',
月补加班= 月补加班 -'+ @jb +',
月休假时间 = 月休假时间 -'+ @gs +'-'+ @jb +'
where (月工时电脑工号 ='+ @j_int +')
AND (月工时日期 like left('+ @s_date +',6))
update '+ @FILENAMEGSY +' set 月补时 = 月补时 + '+ @s_all +',
月补加班= 月补加班 + '+ @s_addh +',
月休假时间 = 月休假时间 +'+ @s_addh +'+ '+ @s_all +'
where (月工时电脑工号 ='+ @j_int +')
AND (月工时日期 like left('+ @s_date +',6))
update '+ @FILENAMEGS +' set 补时 = '+ @s_all +',补加班= '+ @s_addh +'
where (工时电脑工号 ='+ @j_int +') AND (工时日期 like '+ @s_date +')')
end
Else
begin

--**========== @gs,@jb,@j_int 均定义为int类型,不能直接参与字符串运算
exec('update '+ @FILENAMEGS +' set 补时 = '+ @s_all +',补加班='+ @s_addh +'
where (工时电脑工号 ='+ @j_int +') AND (工时日期 like '+ @s_date +')
update '+ @FILENAMEGSY +'
set 月补时 = 月补时 - '+ @gs +'+ '+ @s_all +',
月补加班= 月补加班 - '+ @jb +'+'+ @s_addh +'
where (月工时电脑工号 ='+ @j_int +') AND
(月工时日期 like left('+ @s_date +',6)) ')
end
Fetch next from mycursor Into @gs,@jb,@j_int --**==========应该放在循环中
end
-- Fetch next from mycursor Into @gs,@jb,@j_int --没有继续读取下一条

close mycursor
deallocate mycursor

wea1978 2005-03-10
  • 打赏
  • 举报
回复
哦,谢谢楼上各位大侠。
但我不明白的是,它怎么可以更新到数据呢。
子陌红尘 2005-03-10
  • 打赏
  • 举报
回复
declare @s varchar(4000),@gs int,@jb int,@j_int int

--定义游标
set @s='declare mycursor cursor for
select 补时,补加班,工时电脑工号 from '+ @FILENAMEGS + ' WHERE (工时电脑工号 in(select 工时电脑工号 from temp_selfind) AND (工时日期 like '+ @s_date +'))'
exec(@s)
open mycursor -- 打开游标
fetch next from mycursor into @gs,@jb,@j_int
while @@fetch_status=0
begin
If @Weekday = 0
begin
exec('
update '+ @FILENAMEGSY +' set 月补时 = 月补时 -'+ @gs +',
月补加班= 月补加班 -'+ @jb +',
月休假时间 = 月休假时间 -'+ @gs +'-'+ @jb +'
where (月工时电脑工号 ='+ @j_int +')
AND (月工时日期 like left('+ @s_date +',6))
update '+ @FILENAMEGSY +' set 月补时 = 月补时 + '+ @s_all +',
月补加班= 月补加班 + '+ @s_addh +',
月休假时间 = 月休假时间 +'+ @s_addh +'+ '+ @s_all +'
where (月工时电脑工号 ='+ @j_int +')
AND (月工时日期 like left('+ @s_date +',6))
update '+ @FILENAMEGS +' set 补时 = '+ @s_all +',补加班= '+ @s_addh +'
where (工时电脑工号 ='+ @j_int +') AND (工时日期 like '+ @s_date +')')
end
Else
begin
exec('update '+ @FILENAMEGS +' set 补时 = '+ @s_all +',补加班='+ @s_addh +'
where (工时电脑工号 ='+ @j_int +') AND (工时日期 like '+ @s_date +')
update '+ @FILENAMEGSY +'
set 月补时 = 月补时 - '+ @gs +'+ '+ @s_all +',
月补加班= 月补加班 - '+ @jb +'+'+ @s_addh +'
where (月工时电脑工号 ='+ @j_int +') AND
(月工时日期 like left('+ @s_date +',6)) ')
end
Fetch next from mycursor Into @gs,@jb,@j_int --没有继续读取下一条
end
close mycursor
deallocate mycursor
hsj20041004 2005-03-10
  • 打赏
  • 举报
回复
楼上的对了fetch next from mycursor into @gs,@jb,@j_int语句应该在begin end之间
fetch next from mycursor into @gs,@jb,@j_int
while @@fetch_status=0
begin
...
fetch next from mycursor into @gs,@jb,@j_int
end
close mycursor
deallocate mycursor
pbsql 2005-03-10
  • 打赏
  • 举报
回复
循环好象不对,格式:
...
fetch next from mycursor into @gs,@jb,@j_int
while @@fetch_status=0
begin
...
fetch next from mycursor into @gs,@jb,@j_int
end
close mycursor
deallocate mycursor
...

34,590

社区成员

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

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