techie
techie

Reputation: 467

Change column name in SQL Server 2008

How to change predefined column name to a new name.

eg: Column name is "Accounts"
I want to change it to "A/c"

alter table emp change Accounts....[What next]

Upvotes: 10

Views: 76698

Answers (6)

SantiCarta
SantiCarta

Reputation: 106

Here is the code for sp_rename

sp_RENAME 'emp.Accounts' , 'Acc'

I used something similar and worked

Upvotes: 0

Shivam657
Shivam657

Reputation: 743

The command to rename any column name :

sp_RENAME 'TableName.[OldColumnName]' , 'NewColumnName'

It works without using the 3rd argument 'Column' in the end.

Upvotes: 0

user3124772
user3124772

Reputation: 11

sp_rename 'table_name.accounts', 'A/C', 'column'

this query will solve your problem.

Upvotes: 1

03Usr
03Usr

Reputation: 3435

The script for renaming any column :

sp_RENAME 'TableName.[OldColumnName]' , 'NewColumnName', 'COLUMN'

(Note that you don't use escapes in the second argument, surprisingly.)

The script for renaming any object (table, sp etc) :

sp_RENAME '[OldTableName]' , 'NewTableName'

see here for further info

Upvotes: 20

vijay
vijay

Reputation: 58

You can use sp_rename as:

sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'

as your code is like this:

sp_RENAME 'table.Accounts','Acc','COLUMN'

Upvotes: 0

Peter Schofield
Peter Schofield

Reputation: 993

You need to use the sp_rename command, or use Management Studio to do it visually - make sure you do it at a quiet period, and make sure it has been done in pre-production first with testing!

Incidentally I would keep away from A/C - the slash sign is special meaning division.

The documentation for sp_rename is here, example B is most appropriate. http://msdn.microsoft.com/en-us/library/ms188351.aspx

Upvotes: 13

Related Questions