Why this sql is valid

  • begin

    select ls_action = 0

    end

  • It is just as valid as

    BEGIN

    SELECT 0 AS ls_action

    END

    It is just a different way of writing it.

  • It's the same as:

    BEGIN

    SET ls_action = 0

    END

    -SQLBill

  • As David pointed out,

    begin

    select ls_action = 0

    end

    and

    BEGIN

    SELECT 0 AS ls_action

    END

    are equivalent.  The first form is from very early versions of sql server and dates back to the sybase days.

    Please note that the first form would be the equivalent of a SET statement if ls_action were a variable such as @ls_action.

    As is the first statement is only giving the column name ls_action to the selected value of 0.

    ron

  • From BOL:

    < select_list > ::=

        {    *

            | { table_name | view_name | table_alias }.*

            |     { column_name | expression | IDENTITYCOL | ROWGUIDCOL }

                [ [ AS ] column_alias ]

            | column_alias = expression

        }    [ ,...n ]

     

  • Nope... it's not... SET ls_action=0 will fail in the absence of other code.  You either have to declare ls_action as variable (don't forget to add the @ sign) or you have you have to turn the code into an UPDATE.  But, by itself, SET ls_action=0 will give you an "Incorrect Syntax near "="" message.

    Now, as others have pointed out, SELECT ls_action=0 will work by itself.  It will return a zero in a column named "ls_action" and the equivelent code is SELECT 0 AS ls_action.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply