{"id":82024,"date":"2007-11-04T15:53:59","date_gmt":"2007-11-04T15:53:59","guid":{"rendered":"https:\/\/www.webstaging.red-gate.com\/simple-talk\/?p=73173"},"modified":"2019-05-22T09:10:57","modified_gmt":"2019-05-22T09:10:57","slug":"sys-dm_exec_xml_handles","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/sys-dm_exec_xml_handles\/","title":{"rendered":"sys.dm_exec_xml_handles"},"content":{"rendered":"<p>d Gives you information about any sessions that are using xml documents, including stats about the XML document and usage.\u00a0 You can also get the statements that were used to create the XML handle using sys.dm_exec_sql_text to get the sql.<\/p>\n<p><b>Type:<\/b> Function<br \/>\n<b>Parameter: <\/b>session_id<br \/>\n<b>Data:<\/b> Snapshot, values based on current reality<br \/>\n<b>Columns:<\/b><\/p>\n<ul>\n<li><b>session_id<\/b> &#8211; the session_id of the user who is using the sp_xml_preparedocument command to use the XML datatype<\/li>\n<li><b>document_id<\/b> \u2013 handle created for the document<\/li>\n<li><b>namespace_document_id<\/b> \u2013 you can use sp_xml_preparedocument to declare a document that is\u00a0 namespace, then use it for subsequent calls. This value will have the value of a document_id, or a NULL if there is not one declared<\/li>\n<li><b>sql_handle<\/b> \u2013 relates to sys.dm_exec_sql_text to get the T-SQL of the query<\/li>\n<li><b>statement_start_offset<\/b> &#8211; the starting point in the T-SQL query object that is currently executing (note that these values are double the expected values due to unicode values. You will have to divide by 2 when using them in equations. For more information, see examples)<\/li>\n<li><b>statement_end_offset<\/b> &#8211; the ending point in the T-SQL query object that is currently executing<\/li>\n<li><b>creation_time<\/b> &#8211; the time the handle was created<\/li>\n<li><b>original_document_size_bytes<\/b> \u2013 size of the original text of the XML document<\/li>\n<li><b>original_namespace_document_size_bytes &#8211; <\/b>size of the original text of the XML namespace document<b><\/b><\/li>\n<li><b>num_openxml_calls<\/b> &#8211; Number of times the document has been used in OPENXML calls using this handle<\/li>\n<li><b>row_count<\/b> &#8211; Number of rows that have been returned using this handle in OPENXML calls<\/li>\n<li><b>dormant_duration_ms<\/b> &#8211; the amount of time (in milliseconds) since the last use of the handle in an OPENXML call<\/li>\n<\/ul>\n<p><b>Example:<\/b><\/p>\n<p>DECLARE @idoc int<br \/>\nDECLARE @doc varchar(1000)<\/p>\n<p>SET @doc =&#8217;<br \/>\n&lt;root&gt;<br \/>\n&lt;person firstName=&#8221;barney&#8221; lastName=&#8221;rubble&#8221;\/&gt;<br \/>\n&lt;person firstName=&#8221;fred&#8221; lastName=&#8221;flintstone&#8221;\/&gt;<br \/>\n&lt;\/root&gt;&#8217;<\/p>\n<p>&#8211;Create an internal representation of the XML document.<br \/>\nEXEC sp_xml_preparedocument @idoc OUTPUT, @doc<\/p>\n<p>&#8212; Execute a SELECT statement that uses the OPENXML rowset provider. <br \/>\nSELECT\u00a0\u00a0\u00a0 *<br \/>\nFROM\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 OPENXML (@idoc, &#8216;\/root\/person&#8217;,1)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WITH (firstName varchar(20),<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lastName varchar(20))<\/p>\n<p>Then you can see the information for this document using:<\/p>\n<p>select *, text, substring(text,statement_start_offset \/2,<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1+ statement_end_offset\/2 &#8211; statement_start_offset\/2),<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 statement_start_offset,statement_end_offset<br \/>\nfrom\u00a0\u00a0<strong> sys.dm_exec_xml_handles(null)<\/strong><br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cross apply sys.dm_exec_sql_text(sql_handle)<\/p>\n<p><em>Note:\u00a0 This is part of an ongoing project to write a book about all of the dynamic management views for <\/em><a href=\"http:\/\/www.red-gate.com\/\"><em>Red-Gate<\/em><\/a><em>.\u00a0It will be freely distributable once complete as an ebook.\u00a0Any examples, suggestions, corrections are desired and will certainly be mentioned in the final book.\u00a0 Also, I have begun maintaining the following web page once the book is closer to completion: <\/em><a href=\"https:\/\/www.red-gate.com\/library\/performance-tuning-with-sql-server-dynamic-management-views\"><em>http:\/\/drsql.org\/dmvbook.aspx<\/em><\/a><em>, including a list of resources I have used to write the book so far.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>d Gives you information about any sessions that are using xml documents, including stats about the XML document and usage.\u00a0 You can also get the statements that were used to create the XML handle using sys.dm_exec_sql_text to get the sql. Type: Function Parameter: session_id Data: Snapshot, values based on current reality Columns: session_id &#8211; the&#8230;&hellip;<\/p>\n","protected":false},"author":56085,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[19684],"class_list":["post-82024","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/56085"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=82024"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82024\/revisions"}],"predecessor-version":[{"id":84343,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82024\/revisions\/84343"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=82024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=82024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=82024"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=82024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}