游标内的判断,晕了!求救!代码如下

phyedu 2010-09-09 10:11:01
declare @s varchar(8000),@i int,@ii int,@rm char(10)
declare cur1 cursor local
for
select distinct dw FROM css where xm='100'
open cur1
fetch cur1 into @s
set @i=0
set @ii=1
while @@fetch_status=0
begin
if @i<5
begin
if exists(select * from css where dw=@s and zt='no' )
select top 1 @rm=[id] from css where dw=@s and zt='no'
insert css2(idd,zbh,dww) values(@rm,@ii,@s)
update css set zt='yes' where id=@rm
set @i=@i+1
end
else
set @i=0
set @ii=@ii+1
fetch cur1 into @s
continue
end
close cur1
deallocate cur1
----------------------------------------------------------------
有很多组,每组人数不定,我想重新分组,重新组成5个人的新组,这5个人来至原来不同的组!

...全文
116 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
phyedu 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sqlcenter 的回复:]
引用 7 楼 phyedu 的回复:

服务器: 消息 8108,级别 16,状态 1,行 1
无法使用 SELECT INTO 语句向表 '#temp' 中添加标识列,该表中已有继承了标识属性的列 'bh'。

select rn = identity(int,0,1), id+0 as id, dw into #temp from css where xm='100' and zt ……
[/Quote]
去掉列标识性后,可以执行,但它是第一组循环取,取完不够后,再到下一组,如此循环
phyedu 2010-09-10
  • 打赏
  • 举报
回复
救命啊
SQLCenter 2010-09-09
  • 打赏
  • 举报
回复
见鬼了
另开一个新查询窗的干活
phyedu 2010-09-09
  • 打赏
  • 举报
回复
服务器: 消息 207,级别 16,状态 3,行 3
列名 'id' 无效。
服务器: 消息 207,级别 16,状态 1,行 3
列名 'rn' 无效。
---------------------------
我仔细看了下,这样做应该实现不了目标哦
SQLCenter 2010-09-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 phyedu 的回复:]

服务器: 消息 8108,级别 16,状态 1,行 1
无法使用 SELECT INTO 语句向表 '#temp' 中添加标识列,该表中已有继承了标识属性的列 'bh'。
[/Quote]
select rn = identity(int,0,1), id+0 as id, dw into #temp from css where xm='100' and zt = 'no' order by dw
phyedu 2010-09-09
  • 打赏
  • 举报
回复
服务器: 消息 8108,级别 16,状态 1,行 1
无法使用 SELECT INTO 语句向表 '#temp' 中添加标识列,该表中已有继承了标识属性的列 'bh'。
phyedu 2010-09-09
  • 打赏
  • 举报
回复
嗯,我看看
SQLCenter 2010-09-09
  • 打赏
  • 举报
回复
哪里不全? update?

select rn = identity(int,0,1), id, dw into #temp from css where xm='100' and zt = 'no' order by dw

update css set zt = 'yes' where xm='100' and zt = 'no'

select idd=id, zbh=rn/5+1, dww=dw from #temp
SQLCenter 2010-09-09
  • 打赏
  • 举报
回复
;with cte as
(
select rn=row_number()over(order by dw)-1, id, dw from css where xm='100' and zt='no'
)
select idd=id, zbh=rn/5+1, dww=dw from cte
phyedu 2010-09-09
  • 打赏
  • 举报
回复
大哥,这代码不全?
SQLCenter 2010-09-09
  • 打赏
  • 举报
回复
应该是这个逻辑
;with cte as
(
select rn = row_number() over(order by getdate()) - 1, id,dw from css where xm='100' and zt='no'
)
select idd=id,zbh=rn/5+1, dww=dw from cte
SQLCenter 2010-09-09
  • 打赏
  • 举报
回复
看这个逻辑,不至于需要游标。

34,590

社区成员

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

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