This portal is to share my acquired knowledge related to Sql Server. I hope will be helpful for novice and professionals.

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.

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.
Declare @FareType Char(1)
Set @FareType = 'F'


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: