Wednesday, 22 April 2015

Commands for rename a db a table and a column?

Commands for rename a db, a table and a column?

To rename db
sp_renamedb 'oldname' , 'newname'
If someone is using database it will not accept sp_renmaedb. In that case first bring db to single user using sp_dboptions. Use sp_renamedb to rename database. Use sp_dboptions to bring database to multi user mode.
Example :.
USE master;
GO
EXEC sp_dboption AdventureWorks, 'Single User', True
GO
EXEC sp_renamedb 'AdventureWorks', 'AdventureWorks_New'
GO
EXEC sp_dboption AdventureWorks, 'Single User', False

To rename Table
We can change the table name using sp_rename as follows,
sp_rename 'oldTableName', 'newTableName'

Example :
SP_RENAME 'Table_First', 'Table_Last'
GO

To rename Column
The script for renaming any column :
sp_rename 'TableName.[OldcolumnName]', 'NewColumnName', 'Column'

Example :.
sp_RENAME 'Table_First.Name', 'NameChange' , 'COLUMN'

GO

No comments:

Post a Comment