Cannot assign a default value to a local variable.

Error Message:
Msg 139, Level 15, State 1, Line n
Cannot assign a default value to a local variable.

Severity level:
15.

Description:
This error message appears when you try to assign a value to a variable when you declare it

Consequences:
The SQL statement cannot be parsed and further execution is stopped.

Resolution:
Error of the severity level 15 are generated by the user and can be fixed by the SQL Server user.

Versions:
All versions of SQL Server.

Example(s):
DECLARE @orderid INT = 10000

Remarks:
In contrast to some other programming languages does Transact-SQL not permit to assign a value to a variable at the time of declaration. Declaration and assignment are two distinct operations. To turn the above example into a valid one, you need to separate the declaration from the value assignment like this:
DECLARE @orderid INT
SET @orderid = 10000

]]>

Leave a comment

Your email address will not be published.