Which syntax is better and why?

  • Hello,

    Could someone please educate me on why each of the following two syntaxes would be better than the other:

    SELECT SERVERPROPERTY('productlevel') mycolumnname

    SELECT SERVERPROPERTY('productlevel') as [mycolumnname]

    Thanks!

    Gabor

  • In my openion, second one is better. The key word 'AS' serves as a hint. It is easy to guess that mycolumnname is an alias.

  • Second one...

    It is having more clarity........

  • I agree the second one is much better on the eye

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • I agree mostly... the second one... just loose the square brackets for readability purposes and don't use aliases that require them with the rare exception being reporting queries.

    --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)

  • Just call me lazy to type but I always go for the first one, simply because repeating the word "as" it a lot of typing

    But if I write a query that someone else is going to read, then I normally add the "as" so they can read a little better

    ~PD

  • I don't know... I hate them both...

    I still like

    SELECT

    fieldname = a.field1,

    fieldname2 = a.field2

    FROM

    table1 a

    I know it's not perfect SQL syntax... but the = sign just feels more readable to me.

    So your above would look like this.

    SELECT mycolumnname = SERVERPROPERTY('productlevel')



    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • Now, I agree with THAT! That's my favorite format because it puts all of the aliases in the same position horizontally speaking AND it's the same format required by updates. Makes conversion of a SELECT for test to an actual UPDATE super easy, too!

    Rumor has it that that form is being deprecated and I really, really wish they wouldn't remove it. Leave a good thing be... and, NO, I don't give a rat's patooti if it's ANSI or not.

    --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)

  • Jeff Moden (6/13/2008)


    Now, I agree with THAT! That's my favorite format because it puts all of the aliases in the same position horizontally speaking AND it's the same format required by updates. Makes conversion of a SELECT for test to an actual UPDATE super easy, too!

    Rumor has it that that form is being deprecated and I really, really wish they wouldn't remove it. Leave a good thing be... and, NO, I don't give a rat's patooti if it's ANSI or not.

    I'm sure theres a way to petition to save it... if I had to go back to reading code looking for AS statements to find aliases again... I'd cry...



    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • I prefer [font="courier new"]{expression} AS {columnAlias}[/font], but I suppose its just because I am used to it. I agree with Jeff though, only use the brackets1 when they are needed (column aliases that are keywords or that have non-alpha, non-numeric characters, such as space, etc.).

    (1 It's just "brackets" as there aren't any non-square brackets. "Curly brackets" is a misnomer perpetuated on us by the Illiterati, their real name is "braces".)

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 10 posts - 1 through 9 (of 9 total)

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