Wednesday, June 15, 2011

SQL Selects ='s

Instead of writing a select in a query like this


select
[user].id,
[user].name,
snowboard.name as snowboard
from [user]
inner join snowboard on snowboard.id = [user].snowboard_id


try this


select
[user].id,
[user].name,
snowboard = snowboard.name
from [user]
inner join snowboard on snowboard.id = [user].snowboard_id


It helps all the columns line up and read nicer.

2 comments:

  1. One convention I like to use is putting commas in the select clause at the beginning of the line instead of the end. example:
    select
    [user].id
    ,[user].name
    ,snowboard = snowboard.name

    Helps to spot missing commas since they're all lined up instead of at the end of the line, and allows me to comment out lines easily when debugging without mistakenly generating a SQL syntax error.

    ReplyDelete
  2. Nice.

    I've seen that before, but have resisted it. I think enough people have called me out on it that I should make the change.

    It makes sense. And like you said, it helps with commenting.

    ReplyDelete