{"id":362,"date":"2011-11-30T19:36:10","date_gmt":"2011-11-30T18:36:10","guid":{"rendered":"http:\/\/floris.briolas.nl\/floris\/?p=362"},"modified":"2011-12-08T13:44:11","modified_gmt":"2011-12-08T12:44:11","slug":"sql-server-xml-cheetsheet","status":"publish","type":"post","link":"https:\/\/floris.briolas.nl\/floris\/2011\/11\/sql-server-xml-cheetsheet\/","title":{"rendered":"Sql Server XML Cheetsheet"},"content":{"rendered":"<h1>Cheet sheet XML Features SQL Server<\/h1>\n<p>Pre-requirement: Knowledge of XPath<\/p>\n<p>All the samples are based on this XML:<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>&lt;MyBooks&gt;\r\n\t&lt;ListOwner&gt;John Smith&lt;\/ListOwner&gt;\r\n\t&lt;Book&gt;\r\n\t\t&lt;title published=\"1885\"&gt;The Adventures of Huckleberry Finn&lt;\/title&gt;\r\n\t\t&lt;author realName=\"Samuel Langhorne Clemens\"&gt;Mark Twain&lt;\/author&gt;\r\n\t&lt;\/Book&gt;\r\n\t&lt;Book&gt;\r\n\t\t&lt;title published=\"1894\"&gt;Tom Sawyer Abroad&lt;\/title&gt;\r\n\t\t&lt;author realName=\"Samuel Langhorne Clemens\"&gt;Mark Twain&lt;\/author&gt;\r\n\t&lt;\/Book&gt;\r\n&lt;\/MyBooks&gt;<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Extract Values<br \/>\nselect @xml.value(&#8216;( \/MyBooks\/ListOwner )[1]&#8217;, &#8216;varchar(20)&#8217;);<\/p>\n<p>Possible error:<\/p>\n<table>\n<tbody>\n<tr>\n<th>error<\/th>\n<th>fix<\/th>\n<\/tr>\n<tr>\n<td>&#8216;value()&#8217; requires a singleton (or empty sequence), found operand of type<\/td>\n<td>place the XPATH query between &#8216;()[1]&#8217;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Check if exists<\/p>\n<p>select @xml.exist(&#8216; \/\/Book[author\/@realName=&#8221;Samuel Langhorne Clemens&#8221;] &#8216;)<br \/>\nselect @xml.exist(&#8216; \/\/Book[contains((author\/@realName)[1],&#8221;Langhorne&#8221;)] &#8216;)<\/p>\n<p>Extract Node<\/p>\n<p>SELECT @xml.query(&#8216;\/MyBooks\/Book&#8217;) &#8211;Results all book nodes.SELECT @xml.query(&#8216;\/MyBooks\/Book[title=&#8221;The Adventures of Huckleberry Finn&#8221;]&#8217;)SELECT @xml.query(&#8216;\/MyBooks\/Book[title\/@published&lt;1890]&#8217;)<\/p>\n<p>Select nodes<\/p>\n<p>This will allow you to run SQL queries on XML.<\/p>\n<p>select t.c.value(&#8216;(title)[1]&#8217;,&#8217;varchar(20)&#8217;) from <strong>@xml.nodes(&#8216; \/\/Book &#8216;) as t(c)<\/strong><\/p>\n<p>Modify Xml<\/p>\n<h2>Namespaces:<\/h2>\n<p>When there are namespaces in you XML you will have to do additional work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cheet sheet XML Features SQL Server Pre-requirement: Knowledge of XPath All the samples are based on this XML: &lt;MyBooks&gt; &lt;ListOwner&gt;John Smith&lt;\/ListOwner&gt; &lt;Book&gt; &lt;title published=&#8221;1885&#8243;&gt;The Adventures of Huckleberry Finn&lt;\/title&gt; &lt;author realName=&#8221;Samuel Langhorne Clemens&#8221;&gt;Mark Twain&lt;\/author&gt; &lt;\/Book&gt; &lt;Book&gt; &lt;title published=&#8221;1894&#8243;&gt;Tom Sawyer Abroad&lt;\/title&gt; &lt;author realName=&#8221;Samuel Langhorne Clemens&#8221;&gt;Mark Twain&lt;\/author&gt; &lt;\/Book&gt; &lt;\/MyBooks&gt; Extract Values select @xml.value(&#8216;( \/MyBooks\/ListOwner )[1]&#8217;, &#8216;varchar(20)&#8217;); Possible error: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[3],"tags":[16,17],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p61yPs-5Q","_links":{"self":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts\/362"}],"collection":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/comments?post=362"}],"version-history":[{"count":6,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts\/362\/revisions"}],"predecessor-version":[{"id":369,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/posts\/362\/revisions\/369"}],"wp:attachment":[{"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/media?parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/categories?post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/floris.briolas.nl\/floris\/wp-json\/wp\/v2\/tags?post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}