Wednesday, April 22, 2009

DataBase could not be exclusively Locked when renaming a database

The problem is due to other users using the database. To resolve the problem, you set the database to be used only by yourself. After the rename operation, you need to reset it back to be usable by other users. It is a common courtesy to inform all users prior to doing this operation.

ALTER DATABASE DBNAME SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
SP_RENAMEDB @dbname = 'old_name' ,
@newname = 'new_name'
Go
ALTER DATABASE NEWDBNAME SET MULTI_USER -- set back to multi user
GO