{"id":485,"date":"2015-12-27T21:39:00","date_gmt":"2015-12-28T03:39:00","guid":{"rendered":"https:\/\/jackdonnell.com\/?p=485"},"modified":"2015-12-27T22:22:02","modified_gmt":"2015-12-28T04:22:02","slug":"query-to-view-active-sessions","status":"publish","type":"post","link":"https:\/\/jackdonnell.com\/?p=485","title":{"rendered":"Query to View Active Sessions"},"content":{"rendered":"<p>I want to start out by saying I am not sure about the origin of this query. I use it frequently to find activity on my servers. I left some of the code commented to show a few example items you can add and additional search criteria. I really encourage anyone to grab a query and alter it to show information useful to them.<\/p>\n<p>It uses <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms189747.aspx\" target=\"_blank\">sys.dm_exec_query_plan() <\/a>(via inline select forgive me) to grab the query plan when possible. Additional information to grab performance information and SQL code executing uses these DMVs <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms177648.aspx\" target=\"_blank\">sys.dm_exec_requests<\/a>, <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms179881.aspx\" target=\"_blank\">sys.sysprocesses<\/a> and CROSS APPLY <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms181929.aspx\" target=\"_blank\">sys.dm_exec_sql_text<\/a>().  Use the links to see the information being returned.<\/p>\n<p><!--more--><br \/>\nThe query to view session activity.<\/p>\n<pre lang=\"tsql\" line=\"1\" escaped=\"true\">\r\n\/* Origin Unknown - Adapted by Jack Donnell *\/\r\nUSE master\r\nGO\r\nSET NOCOUNT ON\r\nGO \r\nSELECT \r\nSERVERPROPERTY('ServerName') [Server Name]\r\n,er.session_Id [Spid]\r\n,sp.blocked [Blocked]\r\n-- ,sp.physical_io\r\n-- ,cpu_time\r\n,er.command\r\n,logical_reads\r\n,er.writes\r\n,er.reads\r\n,RTRIM(sp.Hostname) Hostname\r\n,DB_NAME(sp.dbid)[Database]\r\n,RTRIM(sp.loginame)[User]\r\n,er.status [Status]\r\n--,er.command\r\n,er.wait_type [Wait]\r\n,er.wait_time[Wait Time]\r\n-- ,er.[wait_resource]\r\n,CONVERT(VARCHAR(25),er.start_time,22) [Start Time]\r\n,DateDiff(minute,er.start_time,getdate()) [minutes]\r\n,SUBSTRING(qt.text,\r\ner.statement_start_offset\r\n\/ 2,\r\n( CASE WHEN er.statement_end_offset = -1\r\nTHEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2\r\nELSE er.statement_end_offset\r\nEND\r\n- er.statement_start_offset )\r\n\/ 2)[Individual Query]\r\n,qt.text [Parent Query]\r\n,program_name [Program]\r\n,(SELECT TOP 1 CAST (a.query_plan as XML) from sys.dm_exec_query_plan (er.plan_handle) as a) as [QUERY PLAN]\r\n,datediff(mi,start_time,getdate())[Runtime Minutes]\r\n,er.plan_handle\r\nFROM sys.dm_exec_requests er with(NOLOCK)\r\nJOIN sys.sysprocesses sp with(NOLOCK)\r\nON er.session_id = sp.spid\r\nCROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS qt\r\nWHERE 1=1\r\n--AND session_Id = 186\r\nAND session_Id != ( @@SPID ) -- Ignore this current statement.\r\nOPTION (MAXDOP 1);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I want to start out by saying I am not sure about the origin of this query. I use it frequently to find activity on my servers. I left some of the code commented to show a few example items &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/jackdonnell.com\/?p=485\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[266,22,5],"tags":[313,303,315,249,35,36,295,52,310,311,314,312,291],"class_list":["post-485","post","type-post","status-publish","format-standard","hentry","category-dba","category-sql-server","category-t-sql","tag-cross-apply","tag-dba","tag-query","tag-script","tag-select","tag-sql","tag-sql-server","tag-ssms","tag-sys-dm_exec_query_plan","tag-sys-dm_exec_requests","tag-sys-dm_exec_sql_text","tag-sys-sysprocesses","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=485"}],"version-history":[{"count":6,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/485\/revisions"}],"predecessor-version":[{"id":492,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/485\/revisions\/492"}],"wp:attachment":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}