• David Burrows (5/7/2009)


    And, heh... don't get me started on the practice of using leading commas in code.

    Oh! Come on Jeff don't be coy now :w00t:

    For the record, when I code SQL I,

    Use trailing commas

    Aliases when more than one table/subqeury

    Aliases always in subqueries

    Try to make alias meaningful (ie o for Order, ol or OrderLine etc)

    Uppercase SQL keywords

    Indent JOIN beneath FROM

    Indent ON/AND beneath JOIN (each comparison on separate line)

    Indent subqueries and subquery SQL

    I agree with you on most of this...

    However, I don't like all UPPERCASE or lowercase SQL keywords. I prefer mixed case, especially for keywords that are, in fact, multiple words: CharIndex, RaisError (I hate how that is mis-spelled), DatePart, etc. A nested set of functions quickly becomes unreadable when in all uppercase, and is almost as bad in all lowercase.

    I use uppercase to draw attention, ie. select DISTINCT, FOR XML, LEFT OUTER JOIN, etc.

    I also like to spell things out when given a chance, ie. DateAdd(day, 1, GetDate()) vs. DateAdd(dd, 1, GetDate()).

    To add to your list:

    Indent or/and beneath where.

    I try to line up key phrases of select statements and variables in declare statements:

    declare

    @Variable1 tinyint,

    @Variable2 varchar(50)

    select

    @Variable1 = 250,

    @Variable2 = 'Hello World, what have we here?'

    select

    ColumnA,

    ColumnB

    from MyTable

    where ColumnA = @Variable1

    and ColumnB = @Variable2

    To each their own... as long as it's consistent.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2