如何修改1000列中相同的值

python777 2010-04-18 08:52:30
本人在SQL2005中 导入一个表,有近1000列,现在需要把表中第4列到最后列中所有的100改为1000,
用update 改需要给出列名,可是近千列 我不可能都列出。

请高手指教。。
谢谢
...全文
56 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2010-04-18
  • 打赏
  • 举报
回复
declare @s nvarchar(max)
select @s=ISNULL(@s+';','')+
'update Archives set ['+name+']=1000 where ['+name+']=100'
from syscolumns
where id=object_id('Archives')
and colid>=4
exec(@s)
Leshami 2010-04-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xys_777 的回复:]

SQL code

select 'update Archives set '+name+'=1000' from sys.columns where object_id=object_id('Archives')
and column_id>=4

……
[/Quote]
up
喜-喜 2010-04-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xys_777 的回复:]
SQL code

select 'update Archives set '+name+'=1000' from sys.columns where object_id=object_id('Archives')
and column_id>=4

……
[/Quote]

批量转换数据法!学习...
喜-喜 2010-04-18
  • 打赏
  • 举报
回复
--------------------SQL Server数据格式化工具-------------------
---------------------------------------------------------------
-- DESIGNER :happycell188(喜喜)
-- QQ :584738179
-- Development Tool :Microsoft Visual C++ 6.0 C Language
-- FUNCTION :CONVERT DATA TO T-SQL
---------------------------------------------------------------
-- Microsoft SQL Server 2005
-- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
---------------------------------------------------------------
---------------------------------------------------------------


----------------------实例--------------------------
use test
go
if object_id('test.dbo.b') is not null drop table b
-- 创建数据表
create table b
(
公司代码 char(8),
姓名 char(7),
性别 char(3),
工资 int,
a int,
b int,
c int,
d int
)
go
--插入测试数据
insert into b select '100101','张三','男',100,100,100,100,1000
union all select '100101','李叶','女', 2000,45 ,35,345,100
union all select '100102','王小虎','男',100,100,100,353,543
go
--代码实现

select * from b
go
/*原数据
公司代码 姓名 性别 工资 a b c d
----------------------------------------------
100101 张三 男 100 100 100 100 1000
100101 李叶 女 2000 45 353 345 100
100102 王小虎 男 100 100 100 353 543
*/
declare @num int,@i int,@col varchar(30),@str varchar(100)--@num表属性列总数
declare @temp_col_table table(id int identity(1,1),name varchar(100))--创建临时表存储属性列信息
insert into @temp_col_table select name from SysColumns where id=Object_Id('b')
select @num=9,@i=4
while(@i<@num)
begin
select top 1 @col=name from @temp_col_table
where name not in (select top (@i-1) name from @temp_col_table)
exec('update b set '+@col+'=1000 where '+@col+'=100')
set @i=@i+1
end
select * from b
/*update后数据
公司代码 姓名 性别 工资 a b c d
------------------------------------------------------------------
100101 张三 男 1000 1000 1000 1000 1000
100101 李叶 女 2000 45 35 345 1000
100102 王小虎 男 1000 1000 1000 353 543
*/
永生天地 2010-04-18
  • 打赏
  • 举报
回复

select 'update Archives set '+name+'=1000' from sys.columns where object_id=object_id('Archives')
and column_id>=4


---------------------------------------------------------------------------------------------------------------------------------------------------
update tb set ItemCode=1000
update tb set Remark=1000
update tb set Lister=1000
update tb set State=1000

(所影响的行数为 4 行)


最后在吧结果的这些脚本运行一下
喜-喜 2010-04-18
  • 打赏
  • 举报
回复
把表的属性列名放入一个表中,然后遍历表明进行update

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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