Friday, August 14, 2009

SQL - How to rename a DATABASE in MS SQL Server 2005/2008

You could change the DATABASE name by calling sp_renameDB system stored procedure but it will be deprecated in future versions.
EXEC sp_renameDB 'myOldDB','myNewDB'


sp_renamedb (Transact-SQL)
http://msdn.microsoft.com/en-us/library/ms186217.aspx

According to MSDN document in above URL you better Use ALTER DATABASE MODIFY NAME Instead of sp_renameDB to rename.
"This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use ALTER DATABASE MODIFY NAME instead. For more information, see ALTER DATABASE (Transact-SQL)."

ALTER DATABASE (Transact-SQL)
http://msdn.microsoft.com/en-us/library/ms174269.aspx

/* Rename the Database myOldDB to myNewDB */
ALTER DATABASE myOldDB MODIFY NAME = myNewDB
GO


But none of above mentioned commands change Physical Name of database files, how can we change Physical file names of MS SQL Database files (DB and Log files) ? it comes in next blog entry, cheers.
Share/Bookmark

No comments: