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 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
You need to use OFFSET too
Select from
ORDER BY DESC
OFFSET 0 ROWS
FETCH FIRST 1000 ROWS ONLY
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.
Switch partition will work effectively. Read about this
thnks sir………..
Thanks a lot…
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.
here the code ,What is the SP_RENAME ?
It is a system stored procedure which is used to rename the objects
how to apply uper code in C#
You need to ask this in forums like http://www.asp.net
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
You just saved me hours of vague search and trial and error. I used sp_rename like a charm, and it works great. Thanks.
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
what does it mean by sp in sp_RENAME
It means Stored Procedure
Great , worked well….thanks a lot
Not able to change column name as existing column name has Square brackets [ ] say column name is [student_id]
Worked. Thanks!
thanks sir.it’s working
thanksssssssssss
hi;
thanks for simple query
THANX A LOT. IT IS RUNNING AND SO DO MY WORK.
Thanks it worked for me too. And this blog is very informative.
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.
Make use of the result
select ‘exec sp_rename ”’+table_name+’.asset”,”new_name”,”column”’ from information_schema.tables