HomeSQL ServerSQL Server Error Msg 137 – Must declare the scalar variable “%.*ls”

SQL Server Error Msg 137 – Must declare the scalar variable “%.*ls”

In this blog post, let’s learn about the error message “137 – Must declare the scalar variable “%.*ls”.” in Microsoft SQL Server, the reason why it appears and the solution to fix it.

SQL Server Error Message

137 – Must declare the scalar variable “%.*ls”.

Reason for the Error

SQL Server Error Msg 137 occurs when a query or stored procedure references a scalar variable that has not been declared. This error message indicates that the variable must be declared before it can be used in the query or stored procedure.

Here is an example of how this error can occur:

SELECT @counter;

In this example, the code attempts to select the value of a scalar variable called @counter, but the variable has not been declared. This will result in SQL Server throwing an error message similar to the following:

Level 15, State 2, Line 1
Must declare the scalar variable “@counter”.

SQL Server Error Msg 137 - Must declare the scalar variable “%.*ls”

Solution

To resolve this error, declare the variable before it is used in the query or stored procedure. For example:

DECLARE @counter INT = 0;

SELECT @counter;

In this updated example, the variable @counter is declared with an initial value of 0 before it is used in the SELECT statement. The error should no longer occur.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

When dealing with a relational database management system (RDBMS) like SQL Server, compatibility level is an important concept to understand....
In this blog post, let’s learn about the error message “49975 – Unable to load controller client certificate due to...
In this blog post, let’s learn about the error message “49973 – Cannot remove tempdb remote file to local tempdb...