Pages

Saturday, September 27, 2008

Rename Objects in SQL Server

In next couple of posts i will explain about some very usefull stored procedures in SQL Server.

SP_RENAME: SP_RENAME OLD_NAME, NEW_NAME, TYPE_OF_OBJECT

The sp_rename stored procedure is used to modify the names of database objects or user-defined data types. In SQL Server each object is associated with Object ID. When you provide a new name for an existing database object, other database objects using the renamed object continue to refer to the renamed object.

old_name: parameter refers to the existing object name.
new_name: parameter refers to the name you want to assign.
type_of_object: parameter refers to the type of database object whose name you want to modify.

Renaming a table

USE Pubs
GO
EXEC sp_rename 'HR.Employees', 'EMP';
GO

Renaming a column

USE Pubs
GO
EXEC sp_rename 'HR.Employees.EmployeeID', 'EMPID', 'COLUMN';
GO

1 comment:

Jacob Sebastian said...

Please note that it is not recommended to rename a stored procedure, function, view or trigger. After you rename them, sp_helptext and OBJECT_DEFINITION() will still return the object definition with the previous name.

I have explained this in this post: http://www.sqlserverandxml.com/2008/09/be-careful-when-renaming.html