更新多条记录的问题。

mademelaugh 2006-09-07 11:36:57
假设一个表有两个字段id,inum。
数据如下:
1 1
2 null
3 2
4 null
5 3
6 4
7 null
如果想更新inum字段。得到如下结果:
1 1
2 5
3 2
4 6
5 3
6 4
7 7
应该如何解决?(将要更新的记录条数很多)
...全文
183 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoku 2006-09-07
  • 打赏
  • 举报
回复
update test set inum=@i,@i=@i+1 where inum is null


姓义的哥们:
这句绝!
fxf66 2006-09-07
  • 打赏
  • 举报
回复
不好意思 YiZhiNet的对的,本来以为和这个顺序有关
fxf66 2006-09-07
  • 打赏
  • 举报
回复
YiZhiNet(我姓义很多人都觉得奇怪)
update test set inum=@i,@i=@i+1 where inum is null
->
update test set @i=@i+1,inum=@i where inum is null
fxf66 2006-09-07
  • 打赏
  • 举报
回复
declare i int
set @i=(select max(inum) from table)
update table
set @i=@i+1,inum = @i
where inum is null
九斤半 2006-09-07
  • 打赏
  • 举报
回复
create table test(id int identity(1,1),inum int)
insert test(inum) select 1
insert test(inum) select null
insert test(inum) select 2
insert test(inum) select null
insert test(inum) select 3
insert test(inum) select 4
insert test(inum) select null
--select * from test

begin tran
declare @i int
select @i=(select max(inum) from test)
--select @i

update test set inum=@i,@i=@i+1 where inum is null

select * from test
rollback tran

drop table test
mademelaugh 2006-09-07
  • 打赏
  • 举报
回复
3x YiZhiNet(我姓义很多人都觉得奇怪) 。
感谢各位捧场!

34,594

社区成员

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

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