Tuesday, 13 March 2012

Rename a db, a Table and a Column in SQl

What Command do we Use to Rename a db, a Table and a Column?

To Rename db

sp_renamedb ‘oldname’ , ‘newname
If someone is using db it will not accept sp_renmaedb. In that case, first bring db to single user mode using sp_dboptions. Use sp_renamedb to rename the database. Use sp_dboptions to bring the database to multi-user mode.
USE MASTER;GOEXEC sp_dboption AdventureWorks'Single User'True
EXEC sp_renamedb 'AdventureWorks''AdventureWorks_New'GOEXEC sp_dboption AdventureWorks'Single User'False

To Rename Table

We can change the table name using sp_rename as follows:
sp_rename 'oldTableName' 'newTableName'
sp_RENAME 'Table_First''Table_Last'GO

To rename Column

The script for renaming any column is as follows:
sp_rename 'TableName.[OldcolumnName]''NewColumnName''Column'
sp_RENAME 'Table_First.Name''NameChange' 'COLUMN'GO

No comments:

Post a Comment