So yeah, you could use the Microsoft SQL Server Management Studio UI to rename your table or column. But sometimes you need to do the rename in T-SQL. Here’s how.
How to rename a table:
EXEC sp_rename 'OldTableName', 'NewTableName'
How to rename a column:
EXEC sp_rename @objname = 'TableName.OldColumnName', @newname = 'NewColumnName', @objtype = 'COLUMN'
For a more detailed explanation of sp_rename check out this MSDN article: http://msdn2.microsoft.com/en-us/library/ms188351.aspx.
10 thoughts on “How to rename a table or column using T-SQL in Microsoft SQL”
It works crisp and clear!
Great ! Thank you so much
Short and sweet, very nice, thank you!
Thanks, really helped.
Awesome. Visual Studio doesn’t allow you to rename tables, but you can execute a query against the database to perform the rename without needing to load up Management Studio (and possibly attach a .mdf file)
Thanks very much!
Works like a charm, just what I needed.
Thanks a lot!
I used renaming in an edge case recently – to get rid of duplicates. More info on my blog: http://j.mp/gMIUzU