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:

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

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

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

All versions of SQL Server.

DECLARE @orderid INT = 10000

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


No comments yet... Be the first to leave a reply!

Software Reviews | Book Reviews | FAQs | Tips | Articles | Performance Tuning | Audit | BI | Clustering | Developer | Reporting | DBA | ASP.NET Ado | Views tips | | Developer FAQs | Replication Tips | OS Tips | Misc Tips | Index Tuning Tips | Hints Tips | High Availability Tips | Hardware Tips | ETL Tips | Components Tips | Configuration Tips | App Dev Tips | OLAP Tips | Admin Tips | Software Reviews | Error | Clustering FAQs | Performance Tuning FAQs | DBA FAQs |