• Categories

  • Recent Posts

  • Archives

  • Copyright Notice

    Copyright © Nancy Hidy Wilson, 2010-2013. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Nancy Hidy Wilson and nancyhidywilson.wordpress.com with appropriate and specific direction to the original content.

TSQL2sday #20 – T-SQL Best Practices


What is TSQL2sday? Back in late 2009, Adam Machanic (blog | twitter) had this brilliant idea for a monthly SQL Server blogger event (the origin of TSQL2sday) on a unified topic.  This month’s event is hosted by Amit Banerjee (blog | twitter) and the selected topic is “T-SQL Best Practices”.

This will be short and to the point.  My #1 “best practice” tip when writing any code is to include comments! Whether a SELECT statement from a single table or a complex multi-table join using CROSS APPLY, please write a comment stating the objective of the command. You’ll be surprised how soon you forget why you wrote the command in the first place and why you wrote it the way you did.

I’ll open up the proverbial can of worms, though by stating my preferences for when to use block comments /* */ versus dash comments –.

I prefer to use the block method for actual comments.

/* Uncomment the code below in order to list all databases */

I prefer to use the dash comments to comment out actual T-SQL code.

–Select name from sys.databases

What’s your preference for T-SQL comment indicators?

About these ads

4 Responses

  1. I have a very slight preference for dash comments over block comments because frequently when I am working I encounter code outside an editor. Having the leading dash makes it immediately apparent that code is commented out even if there is no highlighting.

    • Hi Vicky,
      Thanks for responding. Yes – there is that consideration. Which is probably why one of my colleagues and I agree to disagree over this preference – he also frequently opens code outside an editor and I very rarely do that! Just depends on your point of view and personal experiences – there is no right answer!

      BTW – It was great to meet you yesterday at the Houston Area SQL Server User Group – hope to see you there again soon!
      Nancy

  2. I learned from several diffferent people long ago never to use — for anything other than single word comments since you never know when your code will be opened in an editor that modifies the breaks in the lines

    –This is a comment

    becomes

    –This is
    a comment
    SYNTAX ERROR!

    {-:

  3. [...] Nancy Hidy Wilson [Blog | Twitter] talks about adding proper comments to your T-SQL code in a crisp and concise [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 416 other followers

%d bloggers like this: