Monday 14 March 2011

T-SQL: How to rename a table or column using T-SQL in Microsoft SQL


So yeah, you could use the Microsoft SQL Server Management Studio UI to rename your table or column. But sometimes you need to do the rename in T-SQL. Here’s how.

How to rename a table:


EXEC sp_rename 'OldTableName','NewTableName'


How to rename a column:

EXEC sp_rename
@objname = 'TableName.OldColumnName',
@newname = 'NewColumnName',
@objtype = 'COLUMN'


For a more detailed explanation of sp_rename check out this MSDN article: http://msdn2.microsoft.com/en-us/library/ms188351.aspx

No comments: