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.

This entry was posted in Web & Software Development and tagged , . Bookmark the permalink.

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

  1. Mandar says:

    Thanks Dusty!

    It works crisp and clear!

    @Mandar~

  2. jefferycxl says:

    Great ! Thank you so much

  3. Renso says:

    Short and sweet, very nice, thank you!

  4. Rytis Usalis says:

    Thanks, really helped.

  5. Steven Blom says:

    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!

  6. MAZ says:

    Works like a charm, just what I needed.
    Thanks a lot!

  7. I used renaming in an edge case recently – to get rid of duplicates. More info on my blog: http://j.mp/gMIUzU

  8. Pingback: How to rename a column in T-SQL | seekway technologies

  9. Shamas says:

    Thanks

  10. leo says:

    Great!
    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>