Solved

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now