{"id":746,"date":"2010-02-01T16:37:00","date_gmt":"2010-02-01T16:37:00","guid":{"rendered":"\/blogs\/paul\/post\/Script-open-transactions-with-text-and-plans.aspx"},"modified":"2017-10-10T07:52:42","modified_gmt":"2017-10-10T14:52:42","slug":"script-open-transactions-with-text-and-plans","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/","title":{"rendered":"Script: open transactions with text and plans"},"content":{"rendered":"<p style=\"text-align: justify;\">[Edit 2017: this query is still entirely applicable on all current versions of SQL Server.]<\/p>\n<p style=\"text-align: justify;\">Here&#8217;s a little script I knocked up this afternoon to tell me who has open transactions on the server &#8211; not just the single oldest active transaction that <em>DBCC OPENTRAN<\/em> returns.<\/p>\n<p>It gives back:<\/p>\n<ul>\n<li>session ID<\/li>\n<li>login name<\/li>\n<li>database context<\/li>\n<li>transaction begin time<\/li>\n<li>how many log records have been generated by the transaction<\/li>\n<li>how much log space has been taken up by those log records<\/li>\n<li>how much log space has been reserved in case the transaction rolls back<\/li>\n<li>the last T-SQL that was executed in the context of the transaction<\/li>\n<li>the last query plan that was executed (only for currently executing plans)<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">It&#8217;s ordered by the transaction begin time. I had some trouble using <em>CROSS APPLY<\/em> with the <em>sys.dm_exec_query_plan<\/em> DMV &#8211; if the plan isn&#8217;t available, it blows out the entire result-set for that transaction. After messing around for ten minutes I discovered the <em>OUTER APPLY<\/em> operator which works nicely with NULL values.<\/p>\n<p style=\"text-align: justify;\">Here&#8217;s the script with some example output (note, there are no delimiters around &#8216;text&#8217; in line 8 as that confuses the code-formatting plugin):<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nSELECT\r\n    &#x5B;s_tst].&#x5B;session_id],\r\n    &#x5B;s_es].&#x5B;login_name] AS &#x5B;Login Name],\r\n    DB_NAME (s_tdt.database_id) AS &#x5B;Database],\r\n    &#x5B;s_tdt].&#x5B;database_transaction_begin_time] AS &#x5B;Begin Time],\r\n    &#x5B;s_tdt].&#x5B;database_transaction_log_bytes_used] AS &#x5B;Log Bytes],\r\n    &#x5B;s_tdt].&#x5B;database_transaction_log_bytes_reserved] AS &#x5B;Log Rsvd],\r\n    &#x5B;s_est].text AS &#x5B;Last T-SQL Text],\r\n    &#x5B;s_eqp].&#x5B;query_plan] AS &#x5B;Last Plan]\r\nFROM\r\n    sys.dm_tran_database_transactions &#x5B;s_tdt]\r\nJOIN\r\n    sys.dm_tran_session_transactions &#x5B;s_tst]\r\nON\r\n    &#x5B;s_tst].&#x5B;transaction_id] = &#x5B;s_tdt].&#x5B;transaction_id]\r\nJOIN\r\n    sys.&#x5B;dm_exec_sessions] &#x5B;s_es]\r\nON\r\n    &#x5B;s_es].&#x5B;session_id] = &#x5B;s_tst].&#x5B;session_id]\r\nJOIN\r\n    sys.dm_exec_connections &#x5B;s_ec]\r\nON\r\n    &#x5B;s_ec].&#x5B;session_id] = &#x5B;s_tst].&#x5B;session_id]\r\nLEFT OUTER JOIN\r\n    sys.dm_exec_requests &#x5B;s_er]\r\nON\r\n    &#x5B;s_er].&#x5B;session_id] = &#x5B;s_tst].&#x5B;session_id]\r\nCROSS APPLY\r\n    sys.dm_exec_sql_text (&#x5B;s_ec].&#x5B;most_recent_sql_handle]) AS &#x5B;s_est]\r\nOUTER APPLY\r\n    sys.dm_exec_query_plan (&#x5B;s_er].&#x5B;plan_handle]) AS &#x5B;s_eqp]\r\nORDER BY\r\n    &#x5B;Begin Time] ASC;\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nsession_id Login Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Database Begin Time\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Log Bytes Log Rsvd Last T-SQL Text\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Last Plan\r\n---------- ----------------- -------- ----------------------- --------- -------- ------------------------------------ ---------\r\n54\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ROADRUNNERPR\\paul foo\u00a0\u00a0\u00a0\u00a0\u00a0 2010-02-01 15:28:48.560 236\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8550\u00a0\u00a0\u00a0\u00a0 begin tran insert into t1 values (1) NULL\r\n55\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ROADRUNNERPR\\paul foo\u00a0\u00a0\u00a0\u00a0\u00a0 2010-02-01 16:38:18.373 356\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8852\u00a0\u00a0\u00a0\u00a0 insert into t1 values (3)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 NULL\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>[Edit 2017: this query is still entirely applicable on all current versions of SQL Server.] Here&#8217;s a little script I knocked up this afternoon to tell me who has open transactions on the server &#8211; not just the single oldest active transaction that DBCC OPENTRAN returns. It gives back: session ID login name database context [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,98],"tags":[],"class_list":["post-746","post","type-post","status-publish","format-standard","hentry","category-example-scripts","category-transaction-log"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Script: open transactions with text and plans - Paul S. Randal<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Script: open transactions with text and plans - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"[Edit 2017: this query is still entirely applicable on all current versions of SQL Server.] Here&#8217;s a little script I knocked up this afternoon to tell me who has open transactions on the server &#8211; not just the single oldest active transaction that DBCC OPENTRAN returns. It gives back: session ID login name database context [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2010-02-01T16:37:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-10-10T14:52:42+00:00\" \/>\n<meta name=\"author\" content=\"Paul Randal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paul Randal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/\",\"name\":\"Script: open transactions with text and plans - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2010-02-01T16:37:00+00:00\",\"dateModified\":\"2017-10-10T14:52:42+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Script: open transactions with text and plans\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\",\"name\":\"Paul S. Randal\",\"description\":\"In Recovery...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\",\"name\":\"Paul Randal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"caption\":\"Paul Randal\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/paul\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Script: open transactions with text and plans - Paul S. Randal","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/","og_locale":"en_US","og_type":"article","og_title":"Script: open transactions with text and plans - Paul S. Randal","og_description":"[Edit 2017: this query is still entirely applicable on all current versions of SQL Server.] Here&#8217;s a little script I knocked up this afternoon to tell me who has open transactions on the server &#8211; not just the single oldest active transaction that DBCC OPENTRAN returns. It gives back: session ID login name database context [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/","og_site_name":"Paul S. Randal","article_published_time":"2010-02-01T16:37:00+00:00","article_modified_time":"2017-10-10T14:52:42+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/","name":"Script: open transactions with text and plans - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2010-02-01T16:37:00+00:00","dateModified":"2017-10-10T14:52:42+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/script-open-transactions-with-text-and-plans\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"Script: open transactions with text and plans"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/","name":"Paul S. Randal","description":"In Recovery...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce","name":"Paul Randal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","caption":"Paul Randal"},"sameAs":["http:\/\/3.209.169.194\/blogs\/paul"],"url":"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/comments?post=746"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/746\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}