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

  • Hi Sir,

    How can we fetch 1000 rows from table?

    I used this but how can I used in store procedure.

    Select from
    ORDER BY DESC
    FETCH FIRST 1000 ROWS ONLY

    Regards
    Dharmendra

    Reply
    • You need to use OFFSET too

      Select from
      ORDER BY DESC
      OFFSET 0 ROWS
      FETCH FIRST 1000 ROWS ONLY

      Reply
  • Hi Sir,

    Which one is faster? Truncating a table and doing a fresh insert of nearly 2million rows OR inserting the 2million into a new table, delete the original table and rename the new table to the original table.
    Oh den do an incremental population of the fulltext index afterwards.

    Reply
  • thnks sir………..

    Reply
  • Thanks a lot…

    Reply
  • Dear Sir,
    Firstly Thanks a lot for your guidance. I am able to write query in sql just because of your notes. Your tutorials are very helpfull for me. I want to go for DBA, but i am not aware how i can achieve it. Please guide me for it and for the oracle certification. So please guide me for it.

    Reply
  • rohol2011d.Rohol Amin
    December 11, 2011 10:37 am

    here the code ,What is the SP_RENAME ?

    Reply
  • rohol2011d.Rohol Amin
    December 11, 2011 10:43 am

    how to apply uper code in C#

    Reply
  • Hi,

    What all can we include in SSAS Cube Design documentation

    I have included the data model,facts,dimensions,surrogate keys,Hierarchies and levels within hierarchies.
    Reports that we can pull from the cube,Proactive caching.

    What else can we include.
    I’m confused.Please help me out.

    Thanks in Advance,
    Samyuktha

    Reply
  • You just saved me hours of vague search and trial and error. I used sp_rename like a charm, and it works great. Thanks.

    Reply
  • hi,
    I had backup using select * into table tablename_bak from tablename

    after that i rename table using your script

    sp_RENAME ‘tablename’, ‘Ttablename_old’
    GO

    sp_RENAME ‘tablename_bak’, ‘Ttablename’
    GO

    when I open from web application the page is white blank,
    I i rename back to original

    sp_RENAME ‘tablename’, ‘Ttablename_bak’
    GO

    sp_RENAME ‘tablename_old’, ‘Ttablename’
    GO

    The page displayed data correctly,

    please advice how this can happen.

    thanks before.
    Liliek

    Reply
  • what does it mean by sp in sp_RENAME

    Reply
  • Great , worked well….thanks a lot

    Reply
  • Not able to change column name as existing column name has Square brackets [ ] say column name is [student_id]

    Reply
  • Worked. Thanks!

    Reply
  • thanks sir.it’s working

    Reply
  • thanksssssssssss

    Reply
  • hi;
    thanks for simple query

    Reply
  • THANX A LOT. IT IS RUNNING AND SO DO MY WORK.

    Reply
  • Thanks it worked for me too. And this blog is very informative.

    Reply
  • Hi Pinal Dave,
    Is there a way to alter columns’ name which contain a certain characters (i.e. ‘asset’) in all tables , instead of altering it manually one by one?

    Thank you.

    Reply
    • Make use of the result

      select ‘exec sp_rename ”’+table_name+’.asset”,”new_name”,”column”’ from information_schema.tables

      Reply

Leave a Reply