{"id":744,"date":"2007-11-20T17:02:00","date_gmt":"2007-11-20T17:02:00","guid":{"rendered":"\/blogs\/bobb\/post\/SQL-Server-2008-XML-Let-there-be-let.aspx"},"modified":"2007-11-20T17:02:00","modified_gmt":"2007-11-20T17:02:00","slug":"sql-server-2008-xml-let-there-be-let","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/","title":{"rendered":"SQL Server 2008 XML: Let there be &#8216;let&#8217;"},"content":{"rendered":"<p>\nSQL Server 2008 adds support for the &#39;let&#39; clause in FLWOR expressions. The for and let clauses have a similar purpose, to bind content (tuples) to variables.&nbsp; Either one can begin a FLWOR expression:\n<\/p>\n<p>\ndeclare @x xml = &#39;&#39;;<br \/>\nselect @x.query(&#39;<br \/>\nfor $i in (1,2,3)<br \/>\nreturn $i<br \/>\n&#39;);<br \/>\n&gt; returns 1 2 3\n<\/p>\n<p>\ndeclare @x xml = &#39;&#39;;<br \/>\nselect @x.query(&#39;<br \/>\nlet $i := (1,2,3)<br \/>\nreturn $i<br \/>\n&#39;);<br \/>\n&gt; returns 1 2 3\n<\/p>\n<p>\nThe distinction is that let is an assignment clause, in the simple statement using &#39;let&#39; above, $i refers to the entire sequence (1,2,3). The for clause sets up an iterator. The simple statement above using &#39;for&#39; loops 3 times and each time through the loop $i refers to a single member of the sequence. So, if I add an &#39;order by&#39; clause, the results are quite different.\n<\/p>\n<p>\ndeclare @x xml = &#39;&#39;;<br \/>\nselect @x.query(&#39;<br \/>\nfor $i in (1,2,3)<br \/>\norder by $i descending<br \/>\nreturn $i<br \/>\n&#39;);<br \/>\n&gt; returns 3 2 1\n<\/p>\n<p>\ndeclare @x xml = &#39;&#39;;<br \/>\nselect @x.query(&#39;<br \/>\nlet $i := (1,2,3)<br \/>\norder by $i descending<br \/>\nreturn $i<br \/>\n&#39;);<br \/>\n&gt; error:<br \/>\n&gt; XQuery [query()]: &#39;order by&#39; requires a singleton (or empty sequence), found operand of type &#39;xs:integer +&#39;\n<\/p>\n<p>\nOne limitation on the XQuery let clause is that it does not support constructed elements. So this statement works fine:\n<\/p>\n<p>\ndeclare @x xml = &#39;&#39;;<br \/>\nselect @x.query(&#39;<br \/>\nlet $x := 1<br \/>\nreturn $x<br \/>\n&#39;);<br \/>\n&gt; returns 1\n<\/p>\n<p>\nbut this statement does not:\n<\/p>\n<p>\ndeclare @x xml = &#39;&#39;;<br \/>\nselect @x.query(&#39;<br \/>\nlet $x := ( &lt;foo&gt;2&lt;\/foo&gt;, &lt;bar&gt;2&lt;\/bar&gt;&nbsp;)<br \/>\nreturn $x<br \/>\n&#39;);<br \/>\n&gt; error:<br \/>\nXQuery [query()]: &#39;let&#39; is not supported with constructed XML\n<\/p>\n<p>\nSo Let the use of the let clause begin&#8230;no longer do I have to explain what a &quot;FWOR&quot; expression is, hooray. However, nota bene. When &#39;let&#39; is used inside a loop, it&#39;s evaluated each time around the loop:\n<\/p>\n<p>\ndeclare @x xml = &#39;&#39;;<br \/>\nselect @x.query(&#39;<br \/>\nfor $i in (1,2,3)<br \/>\nlet $j := 42<br \/>\nreturn ($i, $j)<br \/>\n&#39;);<br \/>\n&gt; returns 1 42 2 42 3 42<br \/>\n&gt; $j is evaluated three times\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL Server 2008 adds support for the &#39;let&#39; clause in FLWOR expressions. The for and let clauses have a similar purpose, to bind content (tuples) to variables.&nbsp; Either one can begin a FLWOR expression: declare @x xml = &#39;&#39;; select @x.query(&#39; for $i in (1,2,3) return $i &#39;); &gt; returns 1 2 3 declare @x [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,37],"tags":[],"class_list":["post-744","post","type-post","status-publish","format-standard","hentry","category-sql-server-2008","category-sql-server-xml"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SQL Server 2008 XML: Let there be &#039;let&#039; - Bob Beauchemin<\/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\/bobb\/sql-server-2008-xml-let-there-be-let\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server 2008 XML: Let there be &#039;let&#039; - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"SQL Server 2008 adds support for the &#039;let&#039; clause in FLWOR expressions. The for and let clauses have a similar purpose, to bind content (tuples) to variables.&nbsp; Either one can begin a FLWOR expression: declare @x xml = &#039;&#039;; select @x.query(&#039; for $i in (1,2,3) return $i &#039;); &gt; returns 1 2 3 declare @x [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2007-11-20T17:02:00+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\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\/bobb\/sql-server-2008-xml-let-there-be-let\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/\",\"name\":\"SQL Server 2008 XML: Let there be 'let' - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2007-11-20T17:02:00+00:00\",\"dateModified\":\"2007-11-20T17:02:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server 2008\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2008\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server 2008 XML: Let there be &#8216;let&#8217;\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server 2008 XML: Let there be 'let' - Bob Beauchemin","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\/bobb\/sql-server-2008-xml-let-there-be-let\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server 2008 XML: Let there be 'let' - Bob Beauchemin","og_description":"SQL Server 2008 adds support for the &#39;let&#39; clause in FLWOR expressions. The for and let clauses have a similar purpose, to bind content (tuples) to variables.&nbsp; Either one can begin a FLWOR expression: declare @x xml = &#39;&#39;; select @x.query(&#39; for $i in (1,2,3) return $i &#39;); &gt; returns 1 2 3 declare @x [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/","og_site_name":"Bob Beauchemin","article_published_time":"2007-11-20T17:02:00+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/","name":"SQL Server 2008 XML: Let there be 'let' - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2007-11-20T17:02:00+00:00","dateModified":"2007-11-20T17:02:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-2008-xml-let-there-be-let\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"SQL Server 2008","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2008\/"},{"@type":"ListItem","position":3,"name":"SQL Server 2008 XML: Let there be &#8216;let&#8217;"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=744"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/744\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}