How to rename a table or column using T-SQL in Microsoft SQL

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

  1. 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!

Leave a Reply to MAZ Cancel reply

Your email address will not be published.