• Skip to main content
  • Skip to primary sidebar

DBA Diaries

Thoughts and experiences of a DBA working with SQL Server and MySQL

How to Rename a SQL Server Column or Table Using T-SQL

November 20, 2013 by Andy Hayes Leave a Comment

I would typically use Management Studio to rename columns or tables in SQL Server. Afterall, it is an easy thing to do to open up the GUI make your changes and then save them back to the SQL Server.

Have you ever been in a situation where you wanted to make a number of such changes and then later on you needed to repeat those changes on another system?

I was in such a situation recently where due to some application changes, many columns had to be renamed, the application tested and then I had to repeat the procedure on production.

I did not want to have to open up every table in Management Studio and click the save button each time all over again!

I needed something that I could execute quickly, so I prepared a file of T-SQL commands to make my changes in one swift stroke. I achieved it all using sp_rename

T-SQL example – renaming columns and tables

So here we have an example for renaming a column:

[sourcecode language=’sql’]
EXEC sp_rename ‘Table.[OldColumnName]’, ‘[NewColumnName]’, ‘[COLUMN]’;
[/sourcecode]

If we wanted to rename a table then we can use:

[sourcecode language=’sql’]
EXEC sp_rename ‘[OldTableName]’, ‘[NewTableName]’;
[/sourcecode]

Note that the square brackets are not mandatory but are necessary when you have silly database designers who have put such things as hyphens or spaces in their field names. Makes me cringe just thinking about it! 😐

Permissions needed for sp_rename

You need ALTER permission if you want to rename tables or columns.

One final note

You can use sp_rename for renaming other objects besides tables. It can also be used for renaming indexes, constraints, types databases and statistics. More more information on this useful little proc, visit this link

Related Posts:

  • How to fix "conversion failed when converting date and/or time from character string"
    How to fix "conversion failed when converting date…
  • sql grouping sets
    Using SQL GROUPING SETS for Multiple GROUP BY…

Filed Under: All Articles, SQL Tips and Tricks Tagged With: sql server, t-sql

About Andy Hayes

Andy Hayes is a DBA working with SQL Server since version 7.0. He has a wonderful wife and two beautiful children. He loves database technology, playing cricket, and blogging. He is passionate about sharing his experiences as a DBA and learning more to further his understanding and knowledge. You can follow me on Twitter, check out my Facebook page or follow me on Google+

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Categories

  • All Articles (84)
  • Career Development (8)
  • MySQL Administration (18)
  • MySQL Performance (2)
  • SQL Server Administration (24)
  • SQL Server News (3)
  • SQL Server Performance (14)
  • SQL Server Security (3)
  • SQL Tips and Tricks (21)

Top 10 Popular Posts

  • Using sp_change_users_login to fix SQL Server orphaned users
  • MySQL SHOW USERS? – How to List All MySQL Users and Privileges
  • How to shrink tempdb
  • How to Transfer Logins to Another SQL Server or Instance
  • How to Delete Millions of Rows using T-SQL with Reduced Impact
  • T-SQL – How to Select Top N Rows for Each Group Using ROW_NUMBER()
  • New T-SQL features in SQL Server 2012 – OFFSET and FETCH
  • How to Kill All MySQL Processes For a Specific User
  • Using exec sp_who2 to help with SQL Server troubleshooting
  • How to fix “conversion failed when converting date and/or time from character string”

Recent Posts

  • How to fix “conversion failed when converting date and/or time from character string”
  • Using SQL GROUPING SETS for Multiple GROUP BY Queries in a Single Query
  • How to Setup MySQL Master Master Replication
  • How To Use SQL to Convert a STRING to an INT
  • How to set up MySQL Replication Tutorial

Search

Connect

  • Twitter
  • Facebook
  • RSS

About

  • Cookie Policy
  • Disclaimer
  • About
Copyright ©