Tuesday, March 20, 2012

An unexplained error message for an Xquery

Hello,
When I issue this query:

select doc.query ('
for $b in /a/b,
$c in /a/c
return $b,$c
')
from T1
where id=6

I recieve this error message:
.Net SqlClient Data Provider: Msg 2227, Level 16, State 1, Line 5
XQuery [T1.doc.query()]: The variable '$c' was not found in the scope in which it was referenced.


Interestingly, when I reverse the order of the variables in the return clause (i.e. make it c$,$b ), the unidentified variable in the error message becomes $b instead of $c. i.e. the system always does not identify the second variable. The query always works fine if the return clause has only one variable, be it $b or $c.

Am I missing something here, or is it a bug?

thanks
-Arsany

The behavior is correct in this case. The problem here is with the precedence of the ',' operator with respect to the FLWOR statement. Basically, the ',' operator in the return statement is not binding to the return clause of the FLWOR, but rather creating a new XQuery expression. If you put parenthesis around the return statement (as shown below) this will enforce the precedence that you want in your query:

select doc.query ('
for $b in /a/b,
$c in /a/c
return ($b,$c)
')
from T1
where id=6
|||Thank you Mike
-Arsany Sawiressql

No comments:

Post a Comment