Twitter Updates

    follow me on Twitter

    Saturday, June 16, 2007

    SQL 2008 - Declaring variables and assigning values in 1 statement

    I just discovered a little nice to have in Katmai (oops, SQL Server 2008). Finally we can declare variables and assign values in one statement.

    If you try this in SQL Server 2005 or earlier:

    declare @myVar VARCHAR(10) = 'Yoohoo';

    This is what you get:

    Msg 139, Level 15, State 1, Line 0
    Cannot assign a default value to a local variable.

    In SQL Server 2008 it just works:

    declare @myVar VARCHAR(10) = 'Yoohoo';

    The cool thing is that it also works for multiple variables:

    declare
    @myVar VARCHAR(10) = 'Yoohoo',
    @myVar2 VARCHAR(10) = 'Cool!!'


    Command(s) completed successfully.

    So ... bye, bye to:

    DECLARE ...

    DECLARE ...

    DECLARE ...

    DECLARE ...

    DECLARE ...

    DECLARE ...


    SET ...

    SET ...

    SET ...

    SET ...

    No comments: