The Result Data Newsletter   
Volume 804 - April 2008   
© Copyright 2008 Result Data Consulting, Ltd.  614-505-0770  www.resultdata.com   

    Result Data Home Page  |  Newsletter Archive  |  Upcoming Events  |  Classes & Workshops  |  Request Information
Upcoming Events:  Mid Ohio BusinessObjects User Group: 5/20, Discover the Power of BusinessObjects XI 3.0:  5/20

Return to Newsletter Contents...

SQL Coding Standards

by: Sean Beal, Senior BI Consultant, CRCP

GETTING STARTED

I’ve heard it said that almost any standard is better than no standard. While I’m not sure I would go that far, I certainly agree with the point that there is no one ‘right way’ to format code.  There are differing opinions here, and the important thing to remember is that a formatting standard is there to make the job easier.  It shouldn’t be so cumbersome as to slow down development.  It shouldn’t be too ‘loose’ or it won’t achieve its goal.  While there might be some pain up front, the results of adopting a well thought out formatting standard will be well worth the effort.

What are the results? It is easier for team members to scan and understand each other’s code.  This improved comprehension allows for team members to troubleshoot and verify code more easily because complex statements are easily identified, modified, or removed.  When code is formatted and consistently applied across the team, development moves more quickly because it is much easier for team members to share the workload and maintain each other’s code.

SOME BASIC FORMATING RULES

·         SQL code should be in all lowercase text with underscore delimitation where necessary
Using all lowercase with underscore delimitation allows you to avoid any case alteration by certain database systems.  Column names and local variables will stand out from SQL keywords, allowing quicker scanning of code.

·         Commas and logical operators (and, or) should begin on a new line
Having commas on the beginning of lines makes it very easy to see when one is missing (one of the more common errors, especially with beginners); beginning logical operations on a new line allow for easy inclusion and removal during testing.

·         Statements that begin a clause (select, from, join, etc.) should begin on a new line; separate clauses with a single blank line

      This allows a developer to more easily locate and identify the key sections of a statement, even if that statement is very long.  Additional clause can also be inserted more easily because each clause is a distinct ‘block’.

·         Information within each clause should be indented

Although we won’t get into the war about whether to use tabs or spaces, indentation helps the main sections stand out (see above), again allowing each clause to stand as a separate code block.

Below is a sample select statement using the above guidelines.

select
   e.employee_id
  ,name_last
  ,e.ssn 
  ,cb.check_batch_id
  ,cb.pay_period_id
  ,pp.date_end period_end_date
  ,ch.check_header_id   
  ,ch.check_number

from   
  check_batch cb

inner join    
  pay_period pp on   
  cb.pay_period_id = pp.pay_period_id

inner join   
  check_header ch on   
  cb.check_batch_id = ch.check_batch_id

inner join   
  employee e on   
  ch.employee_id = e.employee_id   

where   
  cb.is_printed = 1   
  and ch.tax_school_district_withholding != 0

CONCLUSION

While far from a complete specification, the SQL formatting standards above can go a long way towards helping a team function more as a…well, team.  Any new standard is going to be more natural for some and more painful for others, depending on coding habits and training.  However, as the standard is ingrained and becomes habit, the rewards will become apparent, and will be well worth the effort.

Go to Top  |  Return to Newsletter Contents

The Result Data Newsletter is published approximately once a month to share the latest information on business intelligence, data management and CRM. There should be a link below to allow you to change or remove yourself from our list. We take your requests very seriously. If you have any difficulty please contact us at 614-505-0770 and we will make sure that your request is handled properly. This is not intended to be an unsolicited message and you can reach us in person if needed.

© Copyright 2007 Result Data Consulting, Ltd. - All Rights Reserved
All trademarks and copyrights are the property of their respective owners. This information is provided without warranty.
Announcements
Save Big $$ On Gas This Spring
Schedule and attend any one of our qualifying public training classes from April through June 30th to receive up to $385 in gas cards OR  receive 10% off the normal class price.  Please contact our office for further details on promotion amounts and qualifying classes.   Restrictions apply and you must mention the promotional code Gas08 at the time of registration to receive the promotion.  Call 614-505-0770 for further details and restrictions.
Columbus Roadshow - Discover the power of BusinessObjects XI 3.0
See how you could put complete, trustworthy business intelligence into the hands of all of your people wherever their location.  This event will be held on May 20, 2008 at the Fawcett Center.  Call 614-505-0770 or click here for more information and to reserve your seat.
Mid Ohio BusinessObjects User Group
Join us for the Spring MOBOUG User Group meeting on May 20th. Call 614-505-0770 or click here for more information and to reserve your seat.
Looking for a Few Good Men and Women
Join our award winning team of Business Intelligence consultants and .Net Software developers.
Send your resume and salary requirements to:
jobs@resultdata.com