Here I will explain how to get or find number of active connections to database in SQL Server.
In previous articles I explained Read XML file data using SQL Server, Joins in SQL Server, Get Latest Unique Records by count in SQL Server, Insert data with apostrophe in SQL Server, Convert Rows to Columns in SQL and many articles relating to SQL Server. Now I will explain how to get or find number of active connections to database in SQL Server.
To get all information related SQL database connections we can use below queries
Above queries will return information about current users, sessions, and processes in an instance of the Microsoft SQL Server Database Engine. The information can be filtered to return only those processes that are not idle, that belong to a specific user, or that belong to a specific session.
If we run above queries we will get output like as shown below
If we want to see only active connections for specific database we need to write the query like as shown below
FROM MASTER.DBO.SYSPROCESSES WHERE DB_NAME(DBID) = 'your database name' AND DBID != 0
In above query you need to replace 'your database name' with your database name. Once we replace with database name and if we run above query we will get output like as shown below