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.
- Renaming database table column to new name.
- 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.
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
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.
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.
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
You can see the same data now available in new table named “Table_Last”
Reference : Pinal Dave (https://blog.sqlauthority.com)
227 Comments. Leave new
Hi I have renamed the column with following command.
sp_RENAME ‘dbo.tCust.CustomerID’ , ‘[CustID]’, ‘COLUMN’
now it sets the column name [CustID] with braces, now I am unable to remove the braces. Please help me out.
Thanks
Its been solved with following command :)
sp_RENAME ‘dbo.tCust.[[CustID]]]’ , ‘CustID‘, ‘COLUMN’
Thanks for great article
hi,
why u used two and three brackets in this query
sp_RENAME ‘dbo.tCust.[[CustID]]]’ , ‘CustID‘, ‘COLUMN’
Always informative and productive
This is really awesome. i had been searching for this for a long time. thanks a lot
this really good.It worked for me…Thanks
Thanx for this important article…its very helpful
You suck pradheeph.
This is very BORING.
We rockkkk!
it works.. thanks
Hi Pinal,
Could you please help me in below scenario-
I need to modify a column length in a big table (300,000,000) records.
What approach is good-
1. Drop and recreate the table
2. ALTER the column with the new length?
If the column to be altered has index, try disableing the idex and re-create it after column length is modified
I ran this command and it changed the column name fine. But then when I tried to change the column reference in one of my stored procedures, I would get an error that it couldn’t find the column when I ran the ALTER. I even closed the connection and opened it again it still had the same problem. It was really weird, I ended up just recreating the table
Hi Pinal,
Could you please help me with the following?
I’m trying to rename all the column names of my date dimension, from lower case to Title case. There’re around 110 columns, and I don’t want to manually rename them.
Could you please offer the code to do this?
Thanks in advance!
Mate ur simply superb !!!! Terrific Blog Pinal ….
Thanks for exploration ohow torename tables in sql!
Hai thnz dear, itz relly nice knowledge
Thanks, this was very useful!
Only…Thanks.
Useful & Helpful
how to rename one column in sql server 2008?
Use sp_renmae procedure. Also beware that your application may break if you dont modify it.
Muchas Gracias, Thank you, Merci,!