怎么一次性修改多纪录

ak30000 2005-06-29 11:53:45
我需要把某个空字段依次写入10001,10002,10003....升续的数字。
如何一次性完成?最好有实例
...全文
83 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
vivianfdlpw 2005-06-29
  • 打赏
  • 举报
回复
--创建测试环境
set nocount on
go
create table A
(
ID int primary key,
name varchar(20)
)
insert A(ID,name) select 1,null union all select 2,null union all select 3,null

--更新以前
select * from A

select identity(int,10000,1) [tmp],* into # from A
update A set [name]=B.tmp from A join # B on B.[ID]=A.[ID]
drop table #

--更新以后
select * from A

--删除测试环境
drop table A

--结果
/*
ID name
----------- --------------------
1 NULL
2 NULL
3 NULL

ID name
----------- --------------------
1 10000
2 10001
3 10002
*/
vivianfdlpw 2005-06-29
  • 打赏
  • 举报
回复
select identity(int,10000,1) [tmp],* into # from table1
update table1 set [空列]=A.tmp from table1 join # A on A.[主键]=table1.[主键]
drop table #

34,590

社区成员

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

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