I was doing some repairs to a SQL database recently and needed it in single user mode which was the easy part……….USE master; GO ALTER DATABASE dbname SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GOThe issue was then as soon as I went to do my DBCC operation a process had already attached to my database so i found this easy script which drops all open connections to a database:DECLARE @dbname nvarchar(128) SET @dbname = ‘DB name here’ — db to drop connections DECLARE @processid int SELECT @processid = min(spid) FROM master.dbo.sysprocesses WHERE dbid = db_id(@dbname) WHILE @processid IS NOT NULL BEGIN EXEC (‘KILL ‘ + @processid) SELECT @processid = min(spid) FROM master.dbo.sysprocesses WHERE dbid = db_id(@dbname) ENDI was then free to quickly run my additional commands without another process jumping in and grabbing my single user session.
Browse latest info tech news and developments
Our blog publishes bite-sized IT focused articles that offer an easy-to-read insight into ways you can improve your business, communication and operation.
Showcasing developments in the IT industry, practical advice, and time and money saving tips, it’s worth subscribing to stay up to date with the news that matters.