Msg 2227, Level 16, State 1, Line 6
%sThe variable ‘%ls’ was not found in the scope in which it was referenced.
This error message appears when you try to specify a variable in an Xquery, but this variable was not found in the scope in which it was referenced.
The T-SQL statement can be parsed, but causes the error at runtime.
Error of the Severity level 16 are generated by the user and can be fixed by the SQL Server user. The statement cannot be run this way. The variable must exist in the smae scope in which you reference it.
This error message was introduced with SQL Server 2005.
DECLARE @xml xml;
SET @xml = ‘<Result />’;
@xml.query(‘for $a in(1, 2), $b in(3, 4)
$c < $b
return sum($a + $b)’) as XML;
In the above example we try to reference the variable $c in the XQuery. Because this variable is not found in the query’s scope, the error is raised.