How to Retrieve Processes Using Specified Database

If user want to know how many processes are there in any particular database it can be retrieved querying sys.processes database.

USE master
GO
DECLARE @dbid INT
SELECT @dbid = dbid
FROM sys.sysdatabases
WHERE name = 'AdventureWorks'
IF EXISTS (SELECT spid
FROM sys.sysprocesses
WHERE dbid = @dbid)
BEGIN
SELECT 'These processes are using current database' AS Note,
spid, last_batch,
status, hostname, loginame
FROM sys.sysprocesses
WHERE dbid = @dbid
END
GO

Reference:

SQL SERVER – 2005 – Retrieve Processes Using Specified Database

Advertisements

One thought on “How to Retrieve Processes Using Specified Database

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s