%sThe variable '%ls' was not found in the scope in which it was referenced.

Error Message:
Msg 2227, Level 16, State 1, Line 6
%sThe variable ‘%ls’ was not found in the scope in which it was referenced.

Severity level:
16.

Description:
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.

Consequences:
The T-SQL statement can be parsed, but causes the error at runtime.

Resolution:
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.

Versions:
This error message was introduced with SQL Server 2005.

Example(s):
DECLARE @xml xml;
SET @xml = ‘<Result />’;
SELECT
    @xml.query(‘for $a in(1, 2), $b in(3, 4)
    where
        $c < $b
    return sum($a + $b)’) as XML;

Remarks:
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.

]]>

Leave a comment

Your email address will not be published.