| |

VerySource

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

How to quickly change column names

[Copy link]

2

Threads

3

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 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

205

Posts

104

Credits

Newbie

Rank: 1

Credits
104

 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

36

Posts

26

Credits

Newbie

Rank: 1

Credits
26

 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

205

Posts

104

Credits

Newbie

Rank: 1

Credits
104

 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

24

Posts

21

Credits

Newbie

Rank: 1

Credits
21

 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

106

Posts

62

Credits

Newbie

Rank: 1

Credits
62

 China

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

Use magic Report

0

Threads

106

Posts

62

Credits

Newbie

Rank: 1

Credits
62

 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-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list