April 18, 2016 by Kenneth Fisher
Everyone makes mistakes right? And sometimes you create an object and decide later that you messed up and need to rename it.
EXEC sp_rename 'OldName','NewName'
This works for tables, stored procedures, views etc, but there are a few things to be careful about. It doesn’t change the code behind code based objects so you need to modify that as well. And of course any time you use sp_rename you’ll get the warning:
Caution: Changing any part of an object name could break scripts and stored procedures.
This is a good warning, and the longer you’ve had the object around before you change it’s name the more likely that there will be one or more pieces of code that will have to be changed to match the new name.
But right now we are worried about changing the single column of a table. The same warning applies. The longer the column has been around the more likely you will have code that needs to be changed to match. So you want to do this as soon as you possibly can.
CREATE TABLE dbo.ChangeCol (Col1 int, oops int); GO
We still use sp_rename but we have to specify both the table name and column name in the first paramter. The second parameter is just the new column name.
EXEC sp_rename 'dbo.ChangeCol.oops','Col2'; GO