[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

Grant EXECUTE Permissions on all Stored Procedures to a Single User

DECLARE @SQL AS NVARCHAR(MAX);
DECLARE @newline AS NVARCHAR(2);
DECLARE @USER_NAME AS NVARCHAR(100);
DECLARE @sproc_name_pattern AS NVARCHAR(10);

SET @SQL = N”
SET @newline = NCHAR(13) + NCHAR(10);
SET @USER_NAME = N”;

SET @sproc_name_pattern = N’test[_]%’; — Pattern name: select stored procedure which starts with test_

SELECT @SQL = @SQL
+ N’GRANT EXECUTE ON ‘
+ QUOTENAME(OBJECT_SCHEMA_NAME([OBJECT_ID])) + ‘.’
+ QUOTENAME([name])
+ N’ TO ‘
+ QUOTENAME(@USER_NAME)
+ N’;’
+ @newline + @newline
FROM sys.procedures
WHERE [name] LIKE @sproc_name_pattern;

EXEC SP_EXECUTESQL @SQL;

If you have an error says “OBJECT_SCHEMA_NAME …”, you will need to upgrade your SQL Server to 2005 SP2 at least.