27,579
社区成员
发帖
与我相关
我的任务
分享
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)
--------------------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
*/
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 行)
最后在吧结果的这些脚本运行一下