Solved

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

Posted on 2011-09-30
4
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Backup & Restore 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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

752 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