Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename column SQL Server 2008

I am using SQL Server 2008 and Navicat. I need to rename a column in a table using SQL.

ALTER TABLE table_name RENAME COLUMN old_name to new_name; 

This statement doesn't work.

like image 519
Serhio g. Lazin Avatar asked Apr 30 '13 09:04

Serhio g. Lazin


People also ask

How do you change a column name?

To change a column name, enter the following statement in your MySQL shell: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; Replace table_name , old_column_name , and new_column_name with your table and column names.


1 Answers

Use sp_rename

EXEC sp_RENAME 'TableName.OldColumnName' , 'NewColumnName', 'COLUMN' 

See: SQL SERVER – How to Rename a Column Name or Table Name

Documentation: sp_rename (Transact-SQL)

For your case it would be:

EXEC sp_RENAME 'table_name.old_name', 'new_name', 'COLUMN' 

Remember to use single quotes to enclose your values.

like image 62
Habib Avatar answered Sep 20 '22 17:09

Habib