Solved

SQL Server - Script to kill processes associated with a particular database?

Posted on 2011-09-30
4
242 Views
Last Modified: 2012-05-12
I have a routine when updating the backend on a client database:

1) I make the necessary changes to the database on my client's SQL Server.
2) I backup my client's database.
3) I transfer the BAK file to my development server.
4) On my development server, I kill all processes associated with the database.  (Image 1.)  This takes a while.
5) I run a little script on my server to restore the database using the BAK file and set the recovery model to simple.

It would be cool if I could add a little code to my script that kills all processes associated the target database.  This way, step 4 goes away.  Does someone know the TSQL or can they point me to a useful link?
Kill-Process.png
0
Comment
Question by:jdana
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Chris Mangus earned 125 total points
ID: 36895218
How about this?

DECLARE @DatabaseName nvarchar(50)
SET @DatabaseName = N'dbname'

DECLARE @SQL varchar(max)
SET @SQL = ''

SELECT @SQL = @SQL + 'Kill ' + Convert(varchar, SPId) + ';'
FROM MASTER..SysProcesses
WHERE DBId = DB_ID(@DatabaseName) AND SPId <> @@SPId

EXEC(@SQL)


0
 
LVL 5

Assisted Solution

by:AlokJain0412
AlokJain0412 earned 125 total points
ID: 36895686
One another way is that you Can use
As i understood your problem

If you are using SQL 2005 or later,
Use sp_resetstatus 'Database Name'
and
"ALTER DATABASE <dbname> SET ONLINE |OFFLINE.
0
 
LVL 17

Expert Comment

by:Chris Mangus
ID: 36895699
The OP isn't trying to fix a suspect database, they are trying to unconditionally kill connections in a test database before restore.
0
 

Author Closing Comment

by:jdana
ID: 36994818
AlokJain0412,

I see where you're going with the OFFLINE setting.

cmangus,

Couldn't get your code to run, but I have a hunch if I fiddle with it for a while, I'll get it.

Thanks guys.

J
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question