{"id":1418,"date":"2013-10-30T20:33:15","date_gmt":"2013-10-30T14:33:15","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=1418"},"modified":"2016-01-07T11:31:03","modified_gmt":"2016-01-07T05:31:03","slug":"call-smart-sp_who2","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=1418","title":{"rendered":"I call it smart sp_who2"},"content":{"rendered":"<p>If you have been working on SQL Server, you would have definitely used sp_who2. One of the limitations of sp_who2 is that you cannot filter the result set directly and we all know how frustrating that can be sometimes. Obviously there are DMVs you can use to get similar information, but if you are like me and like to use the output of sp_who2 to join with other tables and so on , here a way to filter and query the sp_who2. Basically you create a temporary table and insert the output of sp_who2 to the table and Viola ! Here is how :<\/p>\n<p>DECLARE @Table TABLE(<br \/>\nSPID INT,<br \/>\nStatus VARCHAR(MAX),<br \/>\nLOGIN VARCHAR(MAX),<br \/>\nHostName VARCHAR(MAX),<br \/>\nBlkBy VARCHAR(MAX),<br \/>\nDBName VARCHAR(MAX),<br \/>\nCommand VARCHAR(MAX),<br \/>\nCPUTime INT,<br \/>\nDiskIO INT,<br \/>\nLastBatch VARCHAR(MAX),<br \/>\nProgramName VARCHAR(MAX),<br \/>\nSPID_1 INT,<br \/>\nREQUESTID INT<br \/>\n);<\/p>\n<p>INSERT INTO @Table EXEC sp_who2;<\/p>\n<p>SELECT *<br \/>\nFROM @Table<br \/>\nWHERE \u00c2\u00a0login = &#8216;raju&#8217;<br \/>\norder by DBName;<\/p>\n<p>Now you can join the @table with any other system catalog to get more info or trend patterns.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have been working on SQL Server, you would have definitely used sp_who2. One of the limitations of sp_who2 is that you cannot filter the result set directly and we all know how frustrating that can be sometimes. Obviously there are DMVs you can use to get similar information, but if you are like [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[658],"tags":[865,868,866,867,864,863],"class_list":["post-1418","post","type-post","status-publish","format-standard","hentry","category-sql-server-2","tag-filter-sp_who2","tag-join-sp_who2","tag-query-sp_who2","tag-smart-sp_who2","tag-sp_who2","tag-sql-server-tidbits"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1418"}],"version-history":[{"count":1,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1418\/revisions"}],"predecessor-version":[{"id":1419,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1418\/revisions\/1419"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}