| |

VerySource

 Forgot password?
 Register
Search
View: 763|Reply: 6

How to quickly change column names

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-10 20:00:01
| Show all posts |Read mode
In sql, there are 100 different column names, and I want to add them all to _fw, how can I achieve this quickly.
What about using SQL statements? Or what method?
Such as
a b c d ....
1 1 1 1

I want to become

a_fw b_fw c_fw d_fw ...
1 1 1 1
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-6-2 21:00:01
| Show all posts
select name+'_fw' from syscolumns where id=object_id('table name')
order by colid
Reply

Use magic Report

0

Threads

40

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-6-3 09:30:01
| Show all posts
update t2
set t2.name=t2.name+'_fw'
from sysobjects t1, syscolumns t2
where t1.id=t2.id and t1.xtype='u' and t1.name='Your table name'
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-6-3 11:15:02
| Show all posts
create table T(a int, b int, c int, d int)
insert T select 1, 1, 1,1

declare @sql varchar(8000)
set @sql='select '
select @sql=@sql+name+'_fw='+name+',' from syscolumns where id=object_id('T')
order by colid
select @sql=left(@sql, len(@sql)-1), @sql=@sql+' from T'
--print @sql
exec(@sql)

--result
a_fw b_fw c_fw d_fw
----------- ----------- ----------- -----------
1 1 1 1
Reply

Use magic Report

0

Threads

28

Posts

25.00

Credits

Newbie

Rank: 1

Credits
25.00

 China

Post time: 2020-6-3 19:45:01
| Show all posts
---Set uptable system table
sp_configure'allow updates',1
GO
RECONFIGURE WITH OVERRIDE
GO

update syscolumns set name=name+'_fw' from where id=object_id('table name')


- Remember to set it back in the end.

sp_configure'allow updates', 0
GO
RECONFIGURE WITH OVERRIDE
GO
/*
Otherwise your system is dangerous.
It is recommended that you do not do this.
It is best to use stored procedures or other methods to update.
*/
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-6-3 20:00:02
| Show all posts
Upstairs correct solution, UP
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-6-3 21:00:02
| Show all posts
On the first floor again~
create table T(a int, b int, c int, d int)
insert T select 1, 1, 1,1

declare @sql varchar(8000)
set @sql='select '
select @sql=@sql+name+'_fw='+name+',' from syscolumns where id=object_id('T')
order by colid
select @sql=left(@sql, len(@sql)-1), @sql=@sql+' from T'
--print @sql
exec(@sql)
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list