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.

Here is a video demonstrating the discussion

[youtube=http://www.youtube.com/watch?v=5xviNDISwis]

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 - How to Rename a Column Name or Table Name spRename1

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 - How to Rename a Column Name or Table Name spRename2

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 - How to Rename a Column Name or Table Name spRename3

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 - How to Rename a Column Name or Table Name spRename4

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 - How to Rename a Column Name or Table Name spRename6

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

Reference : Pinal Dave (https://blog.sqlauthority.com)

SQL Scripts, SQL Stored Procedure
Previous Post
SQLAuthority News – Ahmedabad SQL Server User Group Meeting – August 2008
Next Post
SQLAuthority News – SQL Server Express 2008 Downloads

Related Posts

227 Comments. Leave new

  • Shital Kasliwal
    April 23, 2009 4:43 pm

    Hi,
    This Script is Running Correctly.

    Thanks Sir.

    Reply
  • Hi pinal,

    thnx for the info.

    Reply
  • Hi Pinal,
    I think there is no need to pass third parameter as ‘column’ for renaming the column.

    sp_rename ‘Table_First.Name’, NameChange

    i have just tried without mentioning ‘column’ and it works fine.
    May i know what is the importance of the third parameter ‘column’? Is it required or not?

    Reply
    • If you omit, by default it is column. Otherwsie you need to specify where it is column, index, database, etc

      Reply
  • This is really useful info
    Thanks for providing very good and to the point info

    Reply
  • Thanx a lot these is really helpfull, You really can make articles very simple to understand….

    Reply
  • Once again Pinal Dave’s blog to the rescue! Thanks again for a great blog.

    Reply
  • I have used the renaming mistakenly and my column name now looks like this “[column]” without the quotation mark but including the [] charecters. How can I reissue the command to rename the column? what escape sequence should I use.

    Reply
  • i want 2 change a column name that contains a point(.). e.g . using the sp_rename it is giving an error. plz. solve d problem guys

    Reply
    • Plz write this query ur problem will be solved.

      sp_rename ‘[table name].[oldcolumn name]’, ‘newcolumnname’

      Reply
  • I have used the renaming mistakenly and my column name now looks like this “[column]” without the quotation mark but including the [] charecters. How can I reissue the command to rename the column? what escape sequence should I use.

    eg:: if coloumn name is txtname
    after renaming using sp_rename with new coloumn name it is showing [txtname_new].

    Now I cant even retrieve the values for that coloumn.
    Please help me guys on this…

    Thanks in advance.

    Reply
  • @Uttam

    Use the visual editor. Right click on the TABLE and choose “design”. You can change the name there.

    Reply
  • Thanks Brian Sir.
    It really helped.
    Really appreciate your quick help.
    THanks once again.

    Reply
  • thanks sir,
    it was urgent to me find the ans. for my exam

    Reply
  • Thanks for this information

    Reply
  • This is a great find, but it’s not exactly what I need.

    We’re looking at upgrading our servers from 2005 to 2008. This will also involve a name change for our servers. Is there an easy way we can update the cross server links in our views, sp etc withouth having to go into each view and sp?

    Thanks,

    Eloise

    Reply
  • khaled khelawy
    October 28, 2009 1:25 pm

    thx sir

    Reply
  • Thanks pinaldave for a great blog! So much useful information you’ve gained through hard experience you share with us all!

    Here’s a note I would like to add to this article. Do not use square brackets in your target column name or it will put them in there literally! For example:

    sp_rename ‘[my table].[my col]’, ‘[my col2]’, ‘COLUMN’

    is WRONG! Instead say:

    sp_rename ‘[my table].[my col]’, ‘my col2’, ‘COLUMN’

    And what to do if you have columns with brackets that need renaming? You have to use 2 brackets up front and 3 brackets behind!!

    sp_rename ‘[my table].[[my col2]]]’, ‘my col2’, ‘COLUMN’

    Another thing, I figured out this syntax by trial and error (on SQL Server 2008.) It also is needed when doing an aggregate on a column with brackets in its name:

    select SUM([[my col2]]])

    Reply
    • Thank you very much for the “2 brackets up front and 3 brackets behind” discovery!
      The square brackets are actually not needed.
      Another example when google “sp_rename”:
      EXEC sp_rename ‘Sales.SalesTerritory.TerritoryID’, ‘TerrID’, ‘COLUMN’;

      Reply
  • thank u……….

    Reply
  • Can we change the name and the data type of a column at the same time. I know that we can change the data type first and then rename the column. Can we club these 2 actions together in SQL Server 2005?

    Reply
  • Sandhya M. Potdar
    January 4, 2010 3:16 pm

    Pinal Sir,

    Thank you.

    Regards,
    Sandhya M. Potdar.

    Reply
  • thank u sir,
    It is easy to learn.

    Reply

Leave a Reply