check to see if a database is in use

Is there a way to check with a stored procedure if a particular database is in use, and if so kill all the processes.

I would actuallyu like to write this as a stored procedure and call it from visual basic.

is it possible ?
LVL 1
Auerelio VasquezETL DeveloperAsked:
Who is Participating?
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.

Aneesh RetnakaranDatabase AdministratorCommented:
U can use sp_who2
0
Aneesh RetnakaranDatabase AdministratorCommented:
run
'exec sp_who2 '
which lists all the current processes ; Also u can use profiler trace ..

0

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:
To kill all processes against a given db:


ALTER DATABASE databaseName
SET SINGLE_USER WITH ROLLBACK IMMEDIATE --or R.B. AFTER nnn SECONDS
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott PletcherSenior DBACommented:
Then when done doing the processing you need:

ALTER DATABASE databaseName
SET MULTI_USER


You can try KILLing all the connections, but by the time that's done, another connection could have been made.

You could also set the status to OFFLINE, then put it back ONLINE just before whatever it is you want to do -- of course no one could connect to the db while it was OFFLINE.
0
Auerelio VasquezETL DeveloperAuthor Commented:
if i set it to offline, if anyone is using it at that moment, it won't go offline right ?

so, i need to check who is online, then kille that user, then take it offline. is that not right ?

i know how to use sp_who_2, but i want to automate this, and i can't figure out how to kill all users while i'm not sitting at the workstation. if ther are say 9 or 10 uerers, i can use kill for everyone if i'm not watching it, i guess i could use a program to get all the users into an array then kill each user with a for next loop, but i thought there may be something already in place.....

0
Scott PletcherSenior DBACommented:
If you do this:

ALTER DATABASE databaseName
SET OFFLINE WITH ROLLBACK IMMEDIATE

all exising users *will be KILLed immediately* ... however, the rollback may take time if the user was doing something high-volume ... while extremely rare, in theory it could even take hours, depending on the volume of changes and speed of the disk(s) involved.

It might be wise to allow a few seconds for simple trans to finish, like so:

ALTER DATABASE databaseName
SET OFFLINE WITH ROLLBACK AFTER 5 SECONDS
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.