Reputation: 60691
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.
How do I remove all connections from the db?
Upvotes: 13
Views: 21114
Reputation: 1076
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
Upvotes: 0
Reputation: 452988
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
Upvotes: 24
Reputation: 134941
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
Upvotes: 9