{"id":60,"date":"2013-05-31T12:40:00","date_gmt":"2013-05-31T17:40:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=60"},"modified":"2023-07-26T12:33:58","modified_gmt":"2023-07-26T17:33:58","slug":"sql-server-simple-xquery-using-nodes-with-cross-apply","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=60","title":{"rendered":"SQL Server : Simple XQuery Using .nodes() With CROSS APPLY"},"content":{"rendered":"<div>\n<div>\n<div style=\"text-autospace: none;\">\n<span style=\"color: green; font-family: &quot;Courier New&quot;; font-size: 10pt;\">&#8211;simple xquery example utilizing .nodes() with a CROSS APPLY<o:p><\/o:p><\/span><\/div>\n<div style=\"text-autospace: none;\">\n<\/div>\n<div style=\"text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">SELECT<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> <o:p><\/o:p><\/span><\/div>\n<div style=\"text-autospace: none;\">\n<span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\">X<span style=\"color: grey;\">.<\/span>value<span style=\"color: grey;\">(<\/span><span style=\"color: red;\">&#8216;OrderId[1]&#8217;<\/span><span style=\"color: grey;\">,<\/span> <span style=\"color: red;\">&#8216;VARCHAR(25)&#8217;<\/span><span style=\"color: grey;\">)<\/span> <span style=\"color: blue;\">As<\/span> [orderid]<span style=\"color: grey;\">,<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"text-autospace: none;\">\n<span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\">X<span style=\"color: grey;\">.<\/span>value<span style=\"color: grey;\">(<\/span><span style=\"color: red;\">&#8216;Status[1]&#8217;<\/span><span style=\"color: grey;\">,<\/span> <span style=\"color: red;\">&#8216;VARCHAR(10)&#8217;<\/span><span style=\"color: grey;\">)<\/span> <span style=\"color: blue;\">As<\/span> [status]<o:p><\/o:p><\/span><\/div>\n<div style=\"text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">FROM<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> <span style=\"color: green;\">YourTable<\/span><o:p><\/o:p><\/span><\/div>\n<div style=\"text-autospace: none;\">\n<span style=\"color: grey; font-family: &quot;Courier New&quot;; font-size: 10pt;\">CROSS<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> <span style=\"color: grey;\">APPLY<\/span> xmlField<span style=\"color: grey;\">.<\/span>nodes<span style=\"color: grey;\">(<\/span><\/span><span style=\"color: red; font-family: &quot;Courier New&quot;; font-size: 10pt;\">&#8216;head\/body\/response\/orders\/order&#8217;<\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;; font-size: 10pt;\">)<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> <span style=\"color: blue;\">AS<\/span> T<span style=\"color: grey;\">(<\/span>X<span style=\"color: grey;\">)<o:p><\/o:p><\/span><\/span><\/div>\n<div style=\"text-autospace: none;\">\n<span style=\"color: blue; font-family: &quot;Courier New&quot;; font-size: 10pt;\">WHERE<\/span><span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\"> <o:p><\/o:p><\/span><\/div>\n<div style=\"text-autospace: none;\">\n<span style=\"font-family: &quot;Courier New&quot;; font-size: 10pt;\">X<span style=\"color: grey;\">.<\/span>value<span style=\"color: grey;\">(<\/span><span style=\"color: red;\">&#8216;Status[1]&#8217;<\/span><span style=\"color: grey;\">,<\/span> <span style=\"color: red;\">&#8216;VARCHAR(10)&#8217;<\/span><span style=\"color: grey;\">)<\/span> <span style=\"color: grey;\">=<\/span><span style=\"color: red;\">&#8216;O&#8217;<o:p><\/o:p><\/span><\/span><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&#8211;simple xquery example utilizing .nodes() with a CROSS APPLY SELECT X.value(&#8216;OrderId[1]&#8217;, &#8216;VARCHAR(25)&#8217;) As [orderid], X.value(&#8216;Status[1]&#8217;, &#8216;VARCHAR(10)&#8217;) As [status] FROM YourTable CROSS APPLY xmlField.nodes(&#8216;head\/body\/response\/orders\/order&#8217;) AS T(X) WHERE X.value(&#8216;Status[1]&#8217;, &#8216;VARCHAR(10)&#8217;) =&#8216;O&#8217;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,34],"tags":[50,51],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/60"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=60"}],"version-history":[{"count":1,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/60\/revisions\/175"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}