Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming a column in MS SQL Server 2005

Tags:

What is the best practice when it comes to renaming a table column using SQL (MS SQL Server 2005 variant)? This assumes that there is data in the column that must be preserved.

like image 908
Thomas Bratt Avatar asked Aug 10 '09 16:08

Thomas Bratt


People also ask

Can you rename a column in SQL?

It is not possible to rename a column using the ALTER TABLE statement in SQL Server. Use sp_rename instead. To rename a column in SparkSQL or Hive SQL, we would use the ALTER TABLE Change Column command.

How do you title a column in SQL?

Here are the steps explaining how to change column name in SQL by Double click on the column name: Step-1: Follow this path: Databases > Tables > Columns. Step-2: Choose the column name you want to change and then double-click. Step-3: Give a name to your selected column.


1 Answers

You have to use a stored proc to rename a column. The following will rename your column from 'oldColumnName' to 'newColumnName' without affecting any data.

EXEC sp_rename 'tableName.[oldColumnName]', 'newColumnName', 'COLUMN' 

Obviously you'll have to update any code / stored procs / SQL that uses the old name manually.

like image 132
Glen Avatar answered Oct 13 '22 13:10

Glen