关于游标,在线等。。。

hzyzx 2004-03-31 11:26:55
我的代码如下,可能出现死循环,运行没结果,请大家看看,帮忙解决。。
CREATE PROCEDURE [dbo].[d]
AS
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[NEWHL]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[NEWHL]

SELECT *
into dbo.NEWHL
FROM (SELECT GP_SG, gp_cz,GP_DBPF, GP_GYPF, GP_ZTC, GP_ZBP, 'Z' AS gp_zuo,SPACE(40) AS GP_ZLJ1, SPACE(40) AS GP_ZLJ2,SPACE(40) AS GP_YLJ1,SPACE(40) AS GP_YLJ2
FROM dbo.HLZ
WHERE (GP_ZTC > '')
UNION
SELECT gp_sg,gp_cz, gp_dbpf, gp_gypf, gp_ytc, gp_ybp, 'Y' AS gp_zuo,SPACE(40) AS GP_ZLJ1, SPACE(40) AS GP_ZLJ2,SPACE(40) AS GP_YLJ1,SPACE(40) AS GP_YLJ2
FROM hlz
WHERE gp_ytc > '') DERIVEDTBL

--ER TABLE [DBO].NEWHL ADD GP_ZLJ1 CHAR(40) --COLLATE Chinese_PRC_CI_AS NULL
--ALTER TABLE [DBO].NEWHL ADD GP_ZLJ2 CHAR(40) --COLLATE Chinese_PRC_CI_AS NULL
--LTER TABLE [DBO].NEWHL ADD GP_YLJ1 CHAR(40) --COLLATE Chinese_PRC_CI_AS NULL
--LTER TABLE [DBO].NEWHL ADD GP_YLJ2 CHAR(40) --COLLATE Chinese_PRC_CI_AS NULL

alter table dbo.newhl WITH NOCHECK ADD CONSTRAINT pk_newHL
PRIMARY KEY CLUSTERED (gp_cz,gp_dbpf,gp_SG,gp_gypf,gp_zuo)

update newhl set GP_ZLJ1=gp_zbp where gp_zuo='Z' and (len(rtrim(gp_sg))=5)
update newhl set GP_YLJ1=gp_zbp where gp_zuo='Y' and (len(rtrim(gp_sg))=5)

begin
DECLARE @CZ CHAR(30)
DECLARE @DBPF CHAR(40)
DECLARE @GYPF CHAR(50)
DECLARE @ZBP CHAR(20)
declare @ZTC varchar(2)
declare @zuo varchar(2)
DECLARE @SG VARCHAR(6)
DECLARE @LJMX CHAR(40)

declare AA CURSOR FOR
SELECT DISTINCT GP_CZ,GP_DBPF,gp_gypf,GP_ZTC,GP_ZBP FROM NEWHL
open aa
fetch next from aa into @CZ,@DBPF,@GYPF,@ZTC,@ZBP
while @@fetch_status=0
begin
declare bb cursor for select GP_SG from newhl
WHERE GP_CZ=@CZ AND GP_DBPF=@DBPF AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP
SET @LJMX='与'
OPEN BB
FETCH NEXT FROM BB INTO @SG

WHILE @@FETCH_STATUS=0
BEGIN
SET @LJMX=RTRIM(@LJMX)+RTRIM(@SG)
END

FETCH NEXT FROM BB INTO @SG
SET @LJMX=RTRIM(@LJMX) +'同穿'
if @@fetch_status<>0
begin
close bb
deallocate bb
continue
end
close bb
deallocate bb
---------------------------------------------------------------------------
declare bb cursor for select gp_sg,gp_zuo from newhl
where GP_CZ=@CZ AND GP_DBPF=@DBPF AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP
open bb
fetch next from bb into @sg, @zuo
while @@fetch_status =0
begin
if len(rtrim(@sg)) =3
begin
if @zuo='Z'
begin
update newhl set gp_zlj1=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
else
update newhl set gp_ylj1=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
else
begin
if @zuo='Z'
update newhl set gp_zlj2=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
else
update newhl set gp_ylj2=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
end
fetch next from bb into @sg, @zuo
if @@fetch_status<>0
begin
close bb
deallocate bb
continue
end
close bb
deallocate bb
------------------------------------------------------------------------------
end

fetch next from aa into @CZ,@DBPF,@GYPF,@ZTC,@ZBP

close aa
deallocate aa
end
GO
...全文
36 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzyzx 2004-03-31
  • 打赏
  • 举报
回复
GP_SG gp_cz GP_DBPF GP_GYPF GP_ZTC GP_ZBP gp_zuo
134 234 B8-97371-981-6Z AB GHIJ @ SOX-S5X0.3(B) 25 Z
134 234 B8-97371-981-6Z CDEF @ SOX-S5X0.3(B) 25 Z
135 234 B8-97371-981-6Z AB GHIJ @ SOX-S5X0.3(B) 25 Z
135 234 B8-97371-981-6Z CDEF @ SOX-S5X0.3(B) 25 Z
242 234 B8-97371-981-6Z 全品番 ス ES7.44X50(GR) Y
243 234 B8-97371-981-6Z 全品番 ス ES7.44X50(GR) Y
244 234 B8-97371-981-6Z 全品番 ス ES7.44X50(GR) Y
246 234 B8-97371-981-6Z 全品番 シ ES7.44X50(GR) Y
247 234 B8-97371-981-6Z 全品番 シ ES7.44X50(GR) Y
248 234 B8-97371-981-6Z 全品番 シ ES7.44X50(GR) Y
250 234 B8-97371-981-6Z 全品番 サ ES7.44X50(GR) Y
251 234 B8-97371-981-6Z 全品番 サ ES7.44X50(GR) Y
252 234 B8-97371-981-6Z 全品番 サ ES7.44X50(GR) Y
254-A 234 B8-97371-981-6Z 全品番 コ ES7.44X50(GR) Z
255-A 234 B8-97371-981-6Z 全品番 コ ES7.44X50(GR) Z
256-A 234 B8-97371-981-6Z 全品番 コ ES7.44X50(GR) Z
258 234 B8-97371-981-6Z 全品番 ク ES7.44X50(GR) Z
260 234 B8-97371-981-6Z 全品番 ク ES7.44X50(GR) Z
412-A 234 B8-97371-981-6Z CDEFGHIJ セ ES5.72X50(GR) Z
413-A 234 B8-97371-981-6Z CDEFGHIJ セ ES5.72X50(GR) Z
479 234 B8-97371-981-6Z AB GHIJ @ ES7.44X50(GR) Y
这个是测试数据
hzyzx 2004-03-31
  • 打赏
  • 举报
回复
declare AA CURSOR FOR
SELECT DISTINCT GP_CZ,GP_DBPF,gp_gypf,GP_ZTC,GP_ZBP FROM NEWHL
open aa
fetch next from aa into @CZ,@DBPF,@GYPF,@ZTC,@ZBP --从NEWHL中取出没重复的这几个列
while @@fetch_status=0
begin
declare bb cursor for select GP_SG from newhl
WHERE GP_CZ=@CZ AND GP_DBPF=@DBPF AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP
---在aa结果集中可能有多个gp_sg值 ,把gp_sg的值取出来给变量@ljmx
SET @LJMX='与'
OPEN BB
FETCH NEXT FROM BB INTO @SG

WHILE @@FETCH_STATUS=0
BEGIN
SET @LJMX=RTRIM(@LJMX)+RTRIM(@SG)
END

FETCH NEXT FROM BB INTO @SG
SET @LJMX=RTRIM(@LJMX) +'同穿'
---@ljmx实际等于 GP_sg列中多个值相加(一定要读出所有符合BB条件的值)
close bb
deallocate bb
---------------------------------------------------------------------------
declare bb cursor for select gp_sg,gp_zuo from newhl
where GP_CZ=@CZ AND GP_DBPF=@DBPF AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP
---这下面是根据newhl表中gp_sg和gp_zuo 的值更改gp_zlj1,gp_zlj2等列的值
open bb
fetch next from bb into @sg, @zuo
while @@fetch_status =0
begin
if len(rtrim(@sg)) =3
begin
if @zuo='Z'
begin
update newhl set gp_zlj1=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
else
update newhl set gp_ylj1=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
else
begin
if @zuo='Z'
update newhl set gp_zlj2=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
else
update newhl set gp_ylj2=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
end
fetch next from bb into @sg, @zuo

close bb
deallocate bb
------------------------------------------------------------------------------
end

fetch next from aa into @CZ,@DBPF,@GYPF,@ZTC,@ZBP

close aa
deallocate aa
end
GO
1ssp 2004-03-31
  • 打赏
  • 举报
回复
没有说明!!!!
如何关注

hzyzx 2004-03-31
  • 打赏
  • 举报
回复
高手关注哦
hzyzx 2004-03-31
  • 打赏
  • 举报
回复
哈哈,厉害,高手就是高手。。问题解决了。
wyy2004 2004-03-31
  • 打赏
  • 举报
回复
declare AA CURSOR FOR
SELECT DISTINCT GP_CZ,GP_DBPF,gp_gypf,GP_ZTC,GP_ZBP FROM NEWHL
open aa
fetch next from aa into @CZ,@DBPF,@GYPF,@ZTC,@ZBP --从NEWHL中取出没重复的这几个列
while @@fetch_status=0
begin
declare bb cursor for select GP_SG from newhl
WHERE GP_CZ=@CZ AND GP_DBPF=@DBPF AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP
---在aa结果集中可能有多个gp_sg值 ,把gp_sg的值取出来给变量@ljmx
SET @LJMX='与'
OPEN BB
FETCH NEXT FROM BB INTO @SG

WHILE @@FETCH_STATUS=0
BEGIN
SET @LJMX=RTRIM(@LJMX)+RTRIM(@SG)
--END

FETCH NEXT FROM BB INTO @SG
end ----这里加end,否则没循环 ------
SET @LJMX=RTRIM(@LJMX) +'同穿'
---@ljmx实际等于 GP_sg列中多个值相加(一定要读出所有符合BB条件的值)
close bb
deallocate bb
---------------------------------------------------------------------------
declare bb cursor for select gp_sg,gp_zuo from newhl
where GP_CZ=@CZ AND GP_DBPF=@DBPF AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP
---这下面是根据newhl表中gp_sg和gp_zuo 的值更改gp_zlj1,gp_zlj2等列的值
open bb
fetch next from bb into @sg, @zuo
while @@fetch_status =0
begin
if len(rtrim(@sg)) =3
begin
if @zuo='Z'
begin
update newhl set gp_zlj1=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
else
update newhl set gp_ylj1=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
else
begin
if @zuo='Z'
update newhl set gp_zlj2=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
else
update newhl set gp_ylj2=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
--end
fetch next from bb into @sg, @zuo
end ----这里加end,否则没循环
close bb
deallocate bb
------------------------------------------------------------------------------
--end ----这个不要

fetch next from aa into @CZ,@DBPF,@GYPF,@ZTC,@ZBP
end ----这里加end,否则没循环
close aa
deallocate aa
end
GO


wyy2004 2004-03-31
  • 打赏
  • 举报
回复
declare AA CURSOR FOR
SELECT DISTINCT GP_CZ,GP_DBPF,gp_gypf,GP_ZTC,GP_ZBP FROM NEWHL
open aa
fetch next from aa into @CZ,@DBPF,@GYPF,@ZTC,@ZBP --从NEWHL中取出没重复的这几个列
while @@fetch_status=0
begin
declare bb cursor for select GP_SG from newhl
WHERE GP_CZ=@CZ AND GP_DBPF=@DBPF AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP
---在aa结果集中可能有多个gp_sg值 ,把gp_sg的值取出来给变量@ljmx
SET @LJMX='与'
OPEN BB
FETCH NEXT FROM BB INTO @SG

WHILE @@FETCH_STATUS=0
BEGIN
SET @LJMX=RTRIM(@LJMX)+RTRIM(@SG)
END

FETCH NEXT FROM BB INTO @SG
SET @LJMX=RTRIM(@LJMX) +'同穿'
---@ljmx实际等于 GP_sg列中多个值相加(一定要读出所有符合BB条件的值)
close bb
deallocate bb
---------------------------------------------------------------------------
declare bb cursor for select gp_sg,gp_zuo from newhl
where GP_CZ=@CZ AND GP_DBPF=@DBPF AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP
---这下面是根据newhl表中gp_sg和gp_zuo 的值更改gp_zlj1,gp_zlj2等列的值
open bb
fetch next from bb into @sg, @zuo
while @@fetch_status =0
begin
if len(rtrim(@sg)) =3
begin
if @zuo='Z'
begin
update newhl set gp_zlj1=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
else
update newhl set gp_ylj1=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
else
begin
if @zuo='Z'
update newhl set gp_zlj2=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
else
update newhl set gp_ylj2=@ljmx where GP_CZ=@CZ AND GP_DBPF=@DBPF
AND GP_ZTC=@ZTC AND GP_ZBP=@ZBP and gp_sg=@sg and gp_zuo =@zuo
end
end
fetch next from bb into @sg, @zuo

close bb
deallocate bb
------------------------------------------------------------------------------
end

fetch next from aa into @CZ,@DBPF,@GYPF,@ZTC,@ZBP

close aa
deallocate aa
end
GO

hzyzx 2004-03-31
  • 打赏
  • 举报
回复
如果gp_zuo的值='y'and len(gp_sg)=3 更改gp_ylj1
如果gp_zuo的值='y'and len(gp_sg)=5 更改gp_ylj2 这里应该是5
hzyzx 2004-03-31
  • 打赏
  • 举报
回复
如果大家还不清楚的话,我把我的目的再写详细点。。。
现在的目的就是要把gp_cz GP_DBPF GP_GYPF GP_ZTC GP_ZBP这几列数据完全相同
做个更改
如果gp_zuo的值='z'and len(gp_sg)=3 更改gp_zlj1
如果gp_zuo的值='z'and len(gp_sg)=5 更改gp_zlj2
如果gp_zuo的值='y'and len(gp_sg)=3 更改gp_ylj1
如果gp_zuo的值='y'and len(gp_sg)=3 更改gp_ylj2
比如:

gp_sg gp_cz gp_dbpf gp_gypf gp_ztc gp_zbp gp_zuo
242 234 B8-97371-981-6Z 全品番 ス ES7.44X50(GR) Y
243 234 B8-97371-981-6Z 全品番 ス ES7.44X50(GR) Y
244 234 B8-97371-981-6Z 全品番 ス ES7.44X50(GR) Y
我要的结果是:

gp_sg gp_cz gp_dbpf gp_gypf gp_ztc gp_zbp gp_zuo
242 234 B8-97371-981-6Z 全品番 ス ES7.44X50(GR) Y
243 234 B8-97371-981-6Z 全品番 ス ES7.44X50(GR) Y
244 234 B8-97371-981-6Z 全品番 ス ES7.44X50(GR) Y

gp_zlj1 gp_zlj2 gp_ylj1 gp_ylj2 这几列存更该内容
242 与243,244同穿
243 与242,244同穿
244 与242,243同穿





KK4 2004-03-31
  • 打赏
  • 举报
回复
好多呀

22,210

社区成员

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

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