Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close existing connections to a DB

Tags:

sql

sql-server

I want to close the existing connections to an MS SQL Server so that I can do a restore on that database programatically.

like image 504
Haz Avatar asked Apr 30 '12 17:04

Haz


People also ask

How do I close all connections in a database?

Right-click on a database in SSMS and choose delete. In the dialog, check the checkbox for "Close existing connections." Click the Script button at the top of the dialog.

Should I close connection to database?

If you do not close your database connections, many problems can occur like web pages hanging, slow page loads, and more. Think of it as going through a door to your house. Maybe the door will shut by itself, but maybe it won't. If it doesn't shut, who knows what will happen.

How do I disconnect all users from a SQL database?

Right-click the server in Object Explorer and select 'Activity Monitor'. When this opens, expand the Processes group. Now use the drop-down to filter the results by database name. Kill off the server connections by selecting the right-click 'Kill Process' option.


1 Answers

This should disconnect everyone else, and leave you as the only user:

alter database YourDb set single_user with rollback immediate 

Note: Don't forget

alter database YourDb set MULTI_USER 

after you're done!

like image 173
Andomar Avatar answered Sep 26 '22 17:09

Andomar