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

I often get requests from blog reader for T-SQL script to rename database table column name or rename table itself. Both are easy with sp_RENAME, so let me show how to rename a column and a table.

Here is a video demonstrating the discussion

The script for renaming any column :
sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'

The script for renaming any object (table, sp etc) :
sp_RENAME '[OldTableName]' , '[NewTableName]'

This article demonstrates two examples of renaming database object.

  1. Renaming database table column to new name.
  2. Renaming database table to new name.

In both the cases we will first see existing table. Rename the object. Test object again with new name.

1. Renaming database table column to new name.

Example uses AdventureWorks database. A small table with name “Table_First” is created. Table has two fields ID and Name.

SQL Server Management Studio query window.

Now, to change the Column Name from “Name” to “NameChange” we can use command:

USE AdventureWorks
GO
sp_RENAME 'Table_First.Name', 'NameChange' , 'COLUMN'
GO 

Following Fig. show use of SP_RENAME Command

SQL Server Management Studio Message Window.

You can see the column name “Name” is now changed to “NameChange”.

USE AdventureWorks
GO
SELECT *
FROM Table_First
GO

Following fig. verify that the column name has been changed.

SQL Server Results Window.

2.Renaming database table to new name.

We can change the table name too with the same command.

sp_RENAME 'Table_First', 'Table_Last'
GO

Following fig. Shows how we can change Table Name.

SQL Server Management Studio' dialog.

Now, the table name “Table_First” is renamed as “Table_Last”.

“Table_First” will no longer be available in database. We can verify this by running script:

USE AdventureWorks
GO
SELECT *
FROM Table_First
GO

The Messages shows an error “Invalid object name ‘Table_First’.”

To check that the new renamed table exist in database run script:

USE AdventureWorks
GO
SELECT *
FROM Table_Last
GO

SQL Server query tool window with query and execution plan showing.

You can see the same data now available in new table named “Table_Last”

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.

SQL Scripts, SQL Stored Procedure
Previous Post
SQL SERVER – Fix : Error : 40 – could not open a connection to SQL server – Fix Connection Problems of SQL Server
Next Post
SQLAuthority News – Microsoft SQL Server 2008 Report Builder 2.0

Related Posts

227 Comments. Leave new

  • This didn’t specifically solve my issue, but it got me thinking. I was using brackets around the schema and table names. SP_RENAME wasn’t working because what I was sending looked right, but wasn’t. When I did a simple select against the table I found it was schema.[[tablename]] but it looked like schema.[tablename]. I went back and undid the brackets and everything works now. Enjoyed the detail thinking you provided.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.