I need to rename the database, but I cannot since other processes is using it. I don't care about the process, and I need to kill it. 开发者_StackOverflow
How do I remove all connections from the db?
As per my comment once in single_user
mode you need to do the rename from the same connection. Don't try and rename it through Object Explorer as that will try and open up a new connection. The following works this end...
ALTER DATABASE AdventureWorks SET single_user WITH ROLLBACK IMMEDIATE
ALTER DATABASE AdventureWorks MODIFY NAME = NewAdventureWorks
ALTER DATABASE NewAdventureWorks SET multi_user
Like this: Kill All Active Connections To A Database
ALTER DATABASE oldNameSET SINGLE_USER WITH ROLLBACK IMMEDIATE
EXEC sp_renamedb 'oldName', 'newName'
ALTER DATABASE newName SET MULTI_USER --new name of course
Run sp_who, then kill [pid] for everyone in your database.
This worked fine for me to rename database and force disconnect everybody:
ALTER DATABASE [old_name]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [old_name]
MODIFY NAME = [new_name]
GO
ALTER DATABASE [new_name]
SET MULTI_USER
GO
精彩评论