Thursday, June 17, 2010

Cannot assign a default value to a local variable.

This error occurs when we try to assign value to variable at the time of declaration.

See:
Declare @FareType Char(1) = NULL

Here sql server will through that error with severity level 15.
Actually declaration and assignment are both distinct operations.
We can only assign value to variable using SET or SELECT statement.
e.g.
Declare @FareType Char(1)
Set @FareType = 'F'

OR

Declare @FareType Char(1)
Select @FareType = 'F'

But fortuantely MS SQL Server 2008 allows assigning value to variable at the time of declaration .

No comments: