?
Solved

check to see if a database is in use

Posted on 2006-03-27
9
Medium Priority
?
1,093 Views
Last Modified: 2012-06-21
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 ?
0
Comment
Question by:Auerelio Vasquez
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16301187
U can use sp_who2
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1000 total points
ID: 16301204
run
'exec sp_who2 '
which lists all the current processes ; Also u can use profiler trace ..

0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 total points
ID: 16301271
To kill all processes against a given db:


ALTER DATABASE databaseName
SET SINGLE_USER WITH ROLLBACK IMMEDIATE --or R.B. AFTER nnn SECONDS
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 16301296
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
 
LVL 1

Author Comment

by:Auerelio Vasquez
ID: 16301502
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
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 16301734
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Microsoft provides a rich set of technologies for High Availability and Disaster Recovery solutions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

621 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