Indented keywords

viacoboniviacoboni Posts: 13
I prefer this format
SELECT  column1,
        column2
  FROM  T1
  JOIN  T2
    ON  T1.a = T2.a
  LEFT  JOIN T3
    ON  T1.a = T3.a
    AND T2.b = T3.b
  WHERE T1.b = 'some criteria'
    AND T2.b = 'something else'
  GROUP BY column1
  HAVING Count(*) > 1
  ORDER BY column1


The main point here is 2 spaces after SELECT, two-space indent on FROM, JOIN, WHERE, GROUP BY, HAVING, and ORDER BY (main clauses), and an extra two spaces on AND and OR in WHERE and JOIN clauses. Advantage? All the typical code lines up under the column list, without a large indent that visually disconnects keyword from clause. A LEFT JOIN, GROUP BY, or ORDER BY breaks up the column aligning but thats OK.

A major difference from the shipping product is that AND and OR align indented from the keyword, not the parenthesis. If parenthesis are included, aligning from them is fine (but this is rarer than no parenthesis).

If an INSERT...SELECT, the SELECT is indented two spaces and all its subclauses get the extra 2 spaces also.
UPDATE  Table1
  SET   Col1 = Col2
  WHERE Col2 = 'something'

DELETE  Table1
  FROM  Table1
  JOIN  Table2
    ON  Table1.col1 = Table2.col1
  WHERE Table1.Col2 = 'something'  

Any chance we could see this?

Comments

  • This is a rather interesting style I have not seen before. We will consider it in future versions, but it would certainly help to know how widespread this style is. If anyone else is using this style, could you please post a reply to this post.

    Regards,
    Andras
    András Belokosztolszki, PhD
    Red Gate Software Ltd.
  • Hi I use this style:
              SELECT column1
                    ,column2
                    ,column3
                FROM T1
          INNER JOIN T2
                  ON T1.a = T2.a
           LEFT JOIN T3
                  ON T1.a = T3.a
                 AND T2.b = T3.b
     LEFT OUTER JOIN T3
                  ON T1.a = T3.a
                 AND T2.b = T3.b
               WHERE T1.b = 'some criteria'
                 AND T2.b = 'something else'
            GROUP BY column1
                    ,column2
              HAVING Count(*) > 1
            ORDER BY column1
    
    
    Essentially everything aligns on the space after the select statement with enough room for the largest keyword.
  • I use a variation of this. I indent the following lines with a tab, instead of with two spaces:
    INSERT table
        (col1, col2, col3)
        SELECT Val1, Val2, Val3
            FROM table2
            WHERE Condition = true
    
  • myuabovmyuabov Posts: 11 Bronze 3
    I use this format:
    select T1.COL1
         , T1.COL2
         , T2.COL3 
      from T1 
     inner join T2 
        on T1.COL1 = T2.COL1 
      left outer join T3 
        on T1.COL2 = T3.COL2 
       and T2.COL3 = T3.COL3 
     where T1.COL4 = 'some criteria' 
       and T2.COL5 = 'something else' 
     group by T1.COL1
            , T1.COL2
    having count(*) > 1 
     order by T1.COL1, T1.COL2
    
Sign In or Register to comment.