SQL coding techniques

  • GilaMonster (1/8/2012)


    Yes it did (at least on 7, 8i and 9i), and iirc it was just += or maybe (+)= (its been a while), but my comment was just a typo from having spent the morning working on some calculations in Java (x += 5 and the like).

    Oh right, I see! I used += in a SQL Server script today for just the second or third time ever. Just doesn't seem natural in SQL, despite using it in C# all the time. Something like SET @counter += 1 it was.

  • GilaMonster (1/8/2012)


    Yes it did (at least on 7, 8i and 9i), and iirc it was just += or maybe (+)= (its been a while),

    Certainly a (+) on 7 and 8 not sure about 9i though

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • SQL Kiwi (1/8/2012)


    I used += in a SQL Server script today for just the second or third time ever. Just doesn't seem natural in SQL, despite using it in C# all the time. Something like SET @counter += 1 it was.

    I don't think I've ever used it in SQL. C# and Java yes, I always use it there over the x=x+1 form. But not SQL.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Just to add another 2 cents. Readability can be very important. Try going back to some code you wrote a year ago. Keeping in standards in syntax makes the code easier to read and can avoid some costly mistakes. That's why I always INNER JOIN instead of JOIN. I don't bother with the word OUTER since I can clearly see the LEFT word. I've never actually used a RIGHT JOIN in all the years I've been doing SQL. My mind doesn't work that way. (I've only ever seen 2 RIGHT JOINs that were coded by a programmer and not generated by some query GUI type tool.)

    As far as filtering in the JOIN clause. Sometimes this makes sense with INNER JOINS and sometimes it doesn't. I tend to put filtering in JOIN clause if it's really just a subset of rows in the joined to table that I want. Just makes it easier for me to read as I go down writing the query - I've already created the subset with the JOIN and don't have to think about it as I'm joining to more tables further down in the query. (I may have gotten into that habit from LEFT JOINs and making sure I didn't put the condition in the WHERE clause.) I don't know - just easier for me to read and conceptualize as I'm reading down.

    That's just a personal preference and it won't make any difference to the optimizer.

    Todd Fifield

Viewing 4 posts - 16 through 18 (of 18 total)

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