Home » Query connection information in SQL Server

Query connection information in SQL Server

by Vlad Drumea
0 comments

In this post I demo a query to get connection information in SQL Server aggregated by database, host, client and login name.

Intro

I’ve spent some time this week working on the latest release of PSBlitz, and, as part of that, I’ve added 2 new columns to the “Top 10 client connections” table of the “Instance Overview” report page.

For reference, this is how that table looks in the HTML version of the report.


Since I was already working on this, I’ve figured I’d also make the query available in a blog post.
I opted to remove the 10 rows limit here for anyone looking for a query that returns connection information for all the connections on a given SQL Server instance.

Why?

I generally find it useful to see the most active databases and logins, especially for tracking down applications with poor connection management.

Creating a store procedure that inserts the result of the query in a table, and having it run periodically through an Agent job is also a resource friendly option to identify databases that are no longer being used.

The query


You can also find it in my SQL Server Scripts repo as GetClientConnectionInfo.sql

Connection information result set

The result set is pretty self-explanatory, so there’s no need for me to go into details.


The connection details are aggregated per database, login, client host and IP, protocol, and program name to make the result set more manageable on busy instances.

Note that you get trailing “;” if there are only running sessions for a given record.
I can sort this out easily in PowerShell for PSBlitz, but I didn’t want to invest too much time on a Sunday to address it in T-SQL and I also wanted to keep the query fairly small.

Conclusion

I wrote a query and thought I’d share it for anyone who might want to view connection details in SQL Server.

Also, feel free to check out PSBlitz if you haven’t already.

If you’re interested in more utilitarian scripts, you might be interested in:

Or just browse the Scripts category of my blog.

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.