[T-SQL] Rename table or column

reference: http://msdn.microsoft.com/en-us/library/ms188351.aspx

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.

Syntax
sp_rename [ @objname = ] ‘object_name’ , [ @newname = ] ‘new_name’
[ , [ @objtype = ] ‘object_type’ ]

A. Renaming a table
The following example renames the SalesTerritory table to SalesTerr in the Sales schema.

USE AdventureWorks2008R2;
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 AdventureWorks2008R2;
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 AdventureWorks2008R2;
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 AdventureWorks2008R2;
GO
EXEC sp_rename N'Phone', N'Telephone', N'USERDATATYPE';
GO
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s