游标问题,在线等。。

hzyzx 2004-03-30 08:32:31
我有下面一段代码,程序运行结果正确,但是老是在死循环,希望大家看看,谢谢

CREATE PROCEDURE [dbo].[i]
AS

if exists (select gp_cz,gp_dbpf,gp_zdz,gp_zwf,count(gp_zwf) as zong
from newdz where gp_zwf >'**'
group by gp_cz,gp_dbpf,gp_zdz,gp_zwf
having count(gp_zwf)=2)
begin

declare @dz char(20)
declare @wf varchar(2)
declare @cz char(30)
declare @dbpf char(40)
declare @XZXJ char(16)
declare @xzxj1 char(16)
declare @zong int
declare @bh varchar(8)
declare dd cursor for SELECT gp_cz,gp_dbpf,gp_zdz,gp_zwf,count(gp_zwf) as zong
from newdz where gp_zwf >'**'
group by gp_cz,gp_dbpf,gp_zdz,gp_zwf
having count(gp_zwf)=2

open dd
fetch next from dd into @cz,@dbpf,@dz,@wf,@zong
while @@fetch_status=0
begin
declare cc cursor for
select xzxj from newdz where gp_cz=@cz and gp_dbpf=@dbpf and gp_zwf=@wf
open cc
fetch next from cc into @xzxj
while @@fetch_status=0
fetch next from cc into @xzxj1
while @@fetch_status=0
close cc
deallocate cc

declare ee cursor for
select yzk.bh from yzk where yzk.teshu ='双线压着' and yzk.dzmc=@dz and
((yzk.xzxj=@xzxj and yzk.xzxj1=@xzxj1) or (yzk.xzxj=@xzxj1 and yzk.xzxj1=@xzxj))
open ee
fetch next from ee into @bh
while @@fetch_status=0
begin
update newdz set bz=@bh where gp_zwf=@wf and gp_cz=@cz and gp_dbpf=@dbpf -- where current of ee
end
close ee
deallocate ee
end
fetch next from dd into @cz,@dbpf,@dz,@wf,@zong
end
close dd
deallocate dd
GO
...全文
43 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzyzx 2004-03-30
  • 打赏
  • 举报
回复
哈哈,问题解决了,高手就是高手
liuxf0201 2004-03-30
  • 打赏
  • 举报
回复
要取前两个符合条件的数据,
可以用declare cc cursor for
select top 2 xzxj from newdz where gp_cz=@cz and gp_dbpf=@dbpf and gp_zwf=@wf
hzyzx 2004-03-30
  • 打赏
  • 举报
回复
fetch next from cc into @xzxj
while @@fetch_status=0
fetch next from cc into @xzxj1--**********这三句什么意思?????
while @@fetch_status=0
这里是取出前两个符合条件数据
pbsql 2004-03-30
  • 打赏
  • 举报
回复
完整的如下:
CREATE PROCEDURE [dbo].[i]
AS

if exists (select gp_cz,gp_dbpf,gp_zdz,gp_zwf,count(gp_zwf) as zong
from newdz where gp_zwf >'**'
group by gp_cz,gp_dbpf,gp_zdz,gp_zwf
having count(gp_zwf)=2)
begin

declare @dz char(20)
declare @wf varchar(2)
declare @cz char(30)
declare @dbpf char(40)
declare @XZXJ char(16)
declare @xzxj1 char(16)
declare @zong int
declare @bh varchar(8)
declare dd cursor for SELECT gp_cz,gp_dbpf,gp_zdz,gp_zwf,count(gp_zwf) as zong
from newdz where gp_zwf >'**'
group by gp_cz,gp_dbpf,gp_zdz,gp_zwf
having count(gp_zwf)=2

open dd
fetch next from dd into @cz,@dbpf,@dz,@wf,@zong

while @@fetch_status=0
begin
declare cc cursor for
select xzxj from newdz where gp_cz=@cz and gp_dbpf=@dbpf and gp_zwf=@wf
open cc
fetch next from cc into @xzxj
if @@fetch_status=0--********这里不要循环************
fetch next from cc into @xzxj1--************取第二个值*********
if @@fetch_status<>0 --**********这里不要循环,取不到值就退出
begin
close cc
deallocate cc
continue
end
close cc
deallocate cc
declare ee cursor for
select yzk.bh from yzk where yzk.teshu ='双线压着' and yzk.dzmc=@dz and
((yzk.xzxj=@xzxj and yzk.xzxj1=@xzxj1) or (yzk.xzxj=@xzxj1 and yzk.xzxj1=@xzxj))
open ee
fetch next from ee into @bh
while @@fetch_status=0
begin
update newdz set bz=@bh where gp_zwf=@wf and gp_cz=@cz and gp_dbpf=@dbpf -- where current of ee
fetch next from ee into @bh--***********加了这一句以免死循环********
end
close ee
deallocate ee
end
fetch next from dd into @cz,@dbpf,@dz,@wf,@zong
end
close dd
deallocate dd
GO
liuxf0201 2004-03-30
  • 打赏
  • 举报
回复
while @@fetch_status=0
fetch next from cc into @xzxj1
while @@fetch_status=0
close cc
deallocate cc
将close cc前的while @@fetch_status=0 语句去掉
pbsql 2004-03-30
  • 打赏
  • 举报
回复
...
while @@fetch_status=0
begin
declare cc cursor for
select xzxj from newdz where gp_cz=@cz and gp_dbpf=@dbpf and gp_zwf=@wf
open cc
fetch next from cc into @xzxj
while @@fetch_status=0
fetch next from cc into @xzxj1--**********这三句什么意思?????
while @@fetch_status=0 --**********这句死循环了
close cc
deallocate cc

declare ee cursor for
select yzk.bh from yzk where yzk.teshu ='双线压着' and yzk.dzmc=@dz and
((yzk.xzxj=@xzxj and yzk.xzxj1=@xzxj1) or (yzk.xzxj=@xzxj1 and yzk.xzxj1=@xzxj))
open ee
fetch next from ee into @bh
while @@fetch_status=0
begin
update newdz set bz=@bh where gp_zwf=@wf and gp_cz=@cz and gp_dbpf=@dbpf -- where current of ee
fetch next from ee into @bh--***********加了这一句
end
close ee
deallocate ee
end
fetch next from dd into @cz,@dbpf,@dz,@wf,@zong
end
close dd
deallocate dd
GO
zjcxc 2004-03-30
  • 打赏
  • 举报
回复
CREATE PROCEDURE [dbo].[i]
AS

if exists (select gp_cz,gp_dbpf,gp_zdz,gp_zwf,count(gp_zwf) as zong
from newdz where gp_zwf >'**'
group by gp_cz,gp_dbpf,gp_zdz,gp_zwf
having count(gp_zwf)=2)
begin

declare @dz char(20)
declare @wf varchar(2)
declare @cz char(30)
declare @dbpf char(40)
declare @XZXJ char(16)
declare @xzxj1 char(16)
declare @zong int
declare @bh varchar(8)
declare dd cursor for SELECT gp_cz,gp_dbpf,gp_zdz,gp_zwf,count(gp_zwf) as zong
from newdz where gp_zwf >'**'
group by gp_cz,gp_dbpf,gp_zdz,gp_zwf
having count(gp_zwf)=2

open dd
fetch next from dd into @cz,@dbpf,@dz,@wf,@zong
while @@fetch_status=0
begin
declare cc cursor for
select xzxj from newdz where gp_cz=@cz and gp_dbpf=@dbpf and gp_zwf=@wf
open cc
fetch next from cc into @xzxj
while @@fetch_status=0
fetch next from cc into @xzxj1
-- while @@fetch_status=0 --这个你想干什么?
close cc
deallocate cc

declare ee cursor for
select yzk.bh from yzk where yzk.teshu ='双线压着' and yzk.dzmc=@dz and
((yzk.xzxj=@xzxj and yzk.xzxj1=@xzxj1) or (yzk.xzxj=@xzxj1 and yzk.xzxj1=@xzxj))
open ee
fetch next from ee into @bh
while @@fetch_status=0
begin
update newdz set bz=@bh where gp_zwf=@wf and gp_cz=@cz and gp_dbpf=@dbpf -- where current of ee
end
close ee
deallocate ee
end
fetch next from dd into @cz,@dbpf,@dz,@wf,@zong
end
close dd
deallocate dd
GO

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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