Changes the name of a user-created object in the current database. This object can be a table, index, column, alias data type, or Microsoft .NET Framework common language runtime (CLR) user-defined type.
Changing any part of an object name can break scripts and stored procedures. We recommend you do not use this statement to rename stored procedures, triggers, user-defined functions, or views; instead, drop the object and re-create it with the new name.
You can change the name of an object or data type in the current database only. The names of most system data types and system objects cannot be changed.
sp_rename automatically renames the associated index whenever a PRIMARY KEY or UNIQUE constraint is renamed. If a renamed index is tied to a PRIMARY KEY constraint, the PRIMARY KEY constraint is also automatically renamed by sp_rename.
sp_rename can be used to rename primary and secondary XML indexes.
Renaming a stored procedure, function, view, or trigger will not change the name of the corresponding object name in the definition column of the sys.sql_modules catalog view. Therefore, we recommend that sp_rename not be used to rename these object types. Instead, drop and re-create the object with its new name.
Renaming an object such as a table or column will not automatically rename references to that object. You must modify any objects that reference the renamed object manually. For example, if you rename a table column and that column is referenced in a trigger, you must modify the trigger to reflect the new column name. Use sys.sql_expression_dependencies to list dependencies on the object before renaming it.
A. Renaming a table
The following example renames the SalesTerritory table to SalesTerr in the Sales schema.
USE AdventureWorks; GO EXEC sp_rename 'Sales.SalesTerritory', 'SalesTerr'; GO
B. Renaming a column
The following example renames the TerritoryID column in the SalesTerritory table to TerrID.
USE AdventureWorks; GO EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN'; GO
C. Renaming an index
The following example renames the IX_ProductVendor_VendorID index to IX_VendorID.
USE AdventureWorks; GO EXEC sp_rename N'Purchasing.ProductVendor.IX_ProductVendor_VendorID', N'IX_VendorID', N'INDEX'; GO
D. Renaming an alias data type
The following example renames the Phone alias data type to Telephone.
USE AdventureWorks; GO EXEC sp_rename N'Phone', N'Telephone', N'USERDATATYPE'; GO