Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-30
4
Medium Priority
?
246 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 375 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 375 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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

688 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