SQL Server 2008 adds support for the 'let' clause in FLWOR expressions. The for and let clauses have a similar purpose, to bind content (tuples) to variables. Either one can begin a FLWOR expression:
declare @x xml = '';select @x.query('for $i in (1,2,3)return $i');> returns 1 2 3
declare @x xml = '';select @x.query('let $i := (1,2,3)return $i');> returns 1 2 3
The distinction is that let is an assignment clause, in the simple statement using 'let' above, $i refers to the entire sequence (1,2,3). The for clause sets up an iterator. The simple statement above using 'for' loops 3 times and each time through the loop $i refers to a single member of the sequence. So, if I add an 'order by' clause, the results are quite different.
declare @x xml = '';select @x.query('for $i in (1,2,3)order by $i descendingreturn $i');> returns 3 2 1
declare @x xml = '';select @x.query('let $i := (1,2,3)order by $i descendingreturn $i');> error:> XQuery [query()]: 'order by' requires a singleton (or empty sequence), found operand of type 'xs:integer +'
One limitation on the XQuery let clause is that it does not support constructed elements. So this statement works fine:
declare @x xml = '';select @x.query('let $x := 1return $x');> returns 1
but this statement does not:
declare @x xml = '';select @x.query('let $x := ( <foo>2</foo>, <bar>2</bar> )return $x');> error:XQuery [query()]: 'let' is not supported with constructed XML
So Let the use of the let clause begin...no longer do I have to explain what a "FWOR" expression is, hooray. However, nota bene. When 'let' is used inside a loop, it's evaluated each time around the loop:
declare @x xml = '';select @x.query('for $i in (1,2,3)let $j := 42return ($i, $j)');> returns 1 42 2 42 3 42> $j is evaluated three times
Theme design by Jelle Druyts
Pick a theme: BlogXP sqlx BlogXP sqlx
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Bob Beauchemin
E-mail