Solved

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

Posted on 2011-09-30
4
241 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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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