Skip to content

Instantly share code, notes, and snippets.

@donaldgray
Created October 24, 2017 15:34
Show Gist options
  • Save donaldgray/3c903525eedb527f907fdf8c74397374 to your computer and use it in GitHub Desktop.
Save donaldgray/3c903525eedb527f907fdf8c74397374 to your computer and use it in GitHub Desktop.
Script to grab single user mode and set back to multi user. Useful in event of DB in single user but something like polling service grabs connection.
-- Check user with access
SELECT
p.loginame
FROM
sys.databases d
inner join sys.sysprocesses p ON (d.database_id = p.[dbid])
WHERE
d.name = 'database_name'
-- Try to grab database 1000 times
Use [database_name]
GO 1000
-- Set database to multi user mode
ALTER DATABASE [database_name] SET MULTI_USER;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment