How to close all active database connections in one shot?
While trying to restore SQL Server database or do any other actions which require exclusive database access you may face with the following error:
Exclusive access could not be obtained because the database is in use.
Here is a quick solution which is much handier than killing all connections manually from SSMS GUI. Before running the script below make sure that you`re not killing the connections of production users for example or affecting business in any other unwanted way. And don`t forget to put the name of your database to the script.
-- USE [master]
GO
DECLARE @dbname nvarchar(255)
DECLARE @spid int
DECLARE @command nvarchar(300)
/*-------------------------------------
-------Put your database name here ----
--------------------------------------*/
SET @dbname = 'Your database name here'
/*------------------------------------*/
DECLARE dbprocess CURSOR FAST_FORWARD FOR
SELECT sys.databases.name, sys.sysprocesses.spid
FROM sys.sysprocesses INNER JOIN
sys.databases ON sys.sysprocesses.dbid = sys.databases.database_id
WHERE (sys.databases.name = @dbname)
OPEN dbprocess
FETCH NEXT FROM dbprocess INTO @dbname, @spid
WHILE @@FETCH_STATUS = 0
BEGIN
SET @command = ' '
SET @command = 'Kill' +' '+ cast(@spid as nvarchar(5))
EXEC (@command)
FETCH NEXT FROM dbprocess INTO @dbname, @spid
END
CLOSE dbprocess
DEALLOCATE dbprocess
After running the script above you can proceed with the operation which required exclusive access to the database and now it will be successful.
Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.
MoreSubject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:
MoreIn our time of greatest prevalence of databases were relational databases, which are the main targets of the tables and the relationships between them. Tables can solve most problems for data storage and manipulation. But in the real world entity requiring storage is not always presented in a tabular form. One of these very common types of data structures other than the table is a tree structure, where each data element is the parent and the offspring. An example of such a structure may be the structure of state enterprises, which is headed by the director (the root of the tree), his deputies, heads of departments from which are subject to certain deputies, employees of departments, which are subject to the rulers.
MoreSubscribe to our blog
Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.
Subject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:
Discuss your project with us