T-SQL: Quickly Kick All Users Out of a SQL Server Database

Filed under: Quick Code, SQL Server — Written by Chrissy on Tuesday, January 30th, 2007 @ 3:13 pm

There are a few ways to do this but here's one that I like. I believe it works in SQL Server 7, 2000 and 2005.

ALTER DATABASE myDatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
USE myDatabaseName
GO

Go ahead and do what you need to do then set it back:

ALTER DATABASE myDatabaseName SET MULTI_USER
  -

No comments yet. Be the first to comment this post.

Leave your comment