How to kill active sql connection to SQL.

I have a database in SQL. Through SQL Server Management Studio, I am trying to run a script that will kill all active connections to my database. I have Googled it and found a ton of different options, but none of them seem to work. Could I get help with a script that would kill all active connections to a specific database?
brasimanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
What have you tried, and what were the issues?

Kill All Active Connections To A Database
http://wiki.lessthandot.com/index.php/Kill_All_Active_Connections_To_A_Database

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
Don't use KILL.  Just alter the db offline or into single-user mode with rollback immediate:

USE your_db_name
ALTER DATABASE your_db_name SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
--do what you need to do with no other users connected--
ALTER DATABASE your_db_name SET MULTI_USER

If you intend to restore the db, so that you don't need any data/objects in the current db, just drop it instead.

Trying to kill all connections is so SQL 7.0 :-)
Bob LearnedCommented:
Poorly named article.  This is the SQL from the article:

ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK AFTER 60 SECONDS
 
--do you stuff here
 
ALTER DATABASE YourDatabase SET MULTI_USER

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

brasimanAuthor Commented:
What does this do exactly?
ALTER DATABASE your_db_name SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
--do what you need to do with no other users connected--
ALTER DATABASE your_db_name SET MULTI_USER

Rollback doesn't roll any data back does it? Are the connections dropped until they connect back up? How does this work?
Scott PletcherSenior DBACommented:
Rollback rolls back any in-flight transaction(s).  But so would a KILL.  The transactions either have to be committed -- which can happen only when they complete -- or rolled back.  That is the only way to bring the db to a consistent point, which SQL requires under normal conditions.

That command kills all connections other than yours -- i.e., you will be the "single user".

The other existing connection(s) would be dropped.  The affected user might get an error when they retried it, particularly if you are using connection pooling, which is almost a certainty.  But just re-request it and they should be fine.
brasimanAuthor Commented:
Ok, so 1st, i would run this:

USE your_db_name
ALTER DATABASE your_db_name SET SINGLE_USER WITH ROLLBACK IMMEDIATE

Do what I need to do, then i would run:

USE your_db_name
ALTER DATABASE your_db_name SET MULTI_USER

Is that right?
Bob LearnedCommented:
Yes, that is right.
brasimanAuthor Commented:
Thanks everyone!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.