Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Collapse the table of content
Expand the table of content
Expand Minimize


Product Name

SQL Server

Product Version


Product Build Number


Event ID


Event Source




Symbolic Name


Message Text

Must declare the scalar variable "%.*ls".

This error occurs when a variable is used in a SQL script without first declaring the variable. The following example returns error 137 for both the SET and SELECT statements because @mycol is not declared.

SET @mycol = 'ContactName';

SELECT @mycol;

One of the more complicated causes of this error includes the use of a variable that is declared outside the EXECUTE statement. For example, the variable @mycol specified in the SELECT statement is local to the SELECT statement; thus it is outside the EXECUTE statement.

USE AdventureWorks2008R2;


DECLARE @mycol nvarchar(20);

SET @mycol = 'Name';

EXECUTE ('SELECT @mycol FROM Production.Product;');

Verify that any variables used in a SQL script are declared before being used elsewhere in the script.

Rewrite the script so that it does not reference variables in the EXECUTE statement that are declared outside of it. For example:

USE AdventureWorks2008R2;


DECLARE @mycol nvarchar(20) ;

SET @mycol = 'Name';

EXECUTE ('SELECT ' + @mycol + ' FROM Production.Product';) ;

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft