• Lynn Pettis (6/24/2010)


    SanjayAttray (6/24/2010)


    Even though I got it right and know its possible but when executed in SSMS 2005 it returned error.

    Msg 139, Level 15, State 1, Line 0

    Cannot assign a default value to a local variable.

    Msg 139, Level 15, State 1, Line 0

    Cannot assign a default value to a local variable.

    Msg 137, Level 15, State 2, Line 3

    Must declare the scalar variable "@a".

    Code provided in the question is using SQL Server 2008 syntax, that's why it didn't work for you in SQL Server 2005 SSMS.

    Just to make the difference plain.... SQL 2005 requires a separate SET or SELECT to establish the value of a local variable.

    declare @a varchar(6); set @a ='a ';

    declare @b-2 varchar(6); select @b-2 = 'a';

    if @a = @b-2 and @a like @b-2 and @b-2 not like @a print 'zzz'