You will hit this error
Cannot assign a default value to a local variable
in case you are running SQL Server 2005 or earlier, and you try to declare a variable and assign it a value in one statement.
Something like -
DECLARE @Var Varchar(15) = 'Test'
Message 139, Level 15, State 1, Line 0
Cannot assign a default value to a local variable
Variable declaration has been enhanced in SQL Server 2008 and newer versions, and I realized this today when I was deploying a code for multiple systems.
Apparently we also have the flexibility to just use one Declare statement to declare multiple variables -
DECLARE @Var Varchar(15) = 'Test',
@Char Varchar(10) = 'Test2',
@Char2 Varchar(10) = 'Test3'
In SQL Server 2005 or earlier, you will need to declare the variable and then assign it a value using Set statement.
Example -
DECLARE @Var Varchar(15)
SET @Var = 'Test'
SQL SERVER 2005
. It is supported fromSQL SEVER 2008
and above – Ruffle