Everyone knows that we should include comments in our code right? On the other hand the vast majority of us don’t use them as much as we should, myself included. Believe it or not I actually got a job based on the comments I had put into the tech test. It was between me and another person, both of us finished the tech test in about the same amount of time, with code that was similar enough to make for a difficult decision between us. However, I had carefully commented my code (for my own use more than anything) and I ended up with the job. And in case anyone is wondering how I know it was the comments that got me the job, the manager told me after I had been there for a while. On that note here is a brief look at comments in T-SQL.
Here is some common knowledge about comments
--This is a single line comment PRINT 'This is an example ' -- of commenting part of a line -- This is -- An example -- Of commenting -- Out a block -- Of code
/*This is a single line comment*/PRINT 'This is an example ' /* of commenting part of a line*//* This is An example Of commenting Out a block Of code */
Not as common knowledge about comments
This works
/* /* */ */
This does not
/* /* */
This works
-- /* PRINT 'Playing' PRINT 'with' /* PRINT 'block' PRINT 'comments' */
This does not
/* PRINT 'Playing' PRINT 'with' --/* PRINT 'block' PRINT 'comments' */
Some common comment usages
DECLARE @EmplrName varchar(50)-- Employer Name DECLARE @Status varchar(10) -- Hold, Ready, Complete
----------------------------------------------------- -- Declare and initialize variables Code here ----------------------------------------------------- -- Process order information Code here
----------------------------------------------------- --=================================================== --*************************************************** /***************************************************/
A couple of comment lines, maybe with a descriptive comment in the middle, can provide extra emphasis to a section of code.
--=================================================== --=========== Load and process employees ============ --===================================================
-- This is a loop WHILE 1=1 BEGIN -- This is what the loop does PRINT 1 END
/************************************************************* ** This stored procedure is really important. It does stuff ** Parameter list ** @StuffToDo -- What do you want the stored procedure to do ** @WhenToDoIt -- When do you want it done ************************************************************** ** 10/20/2000 - Fixed bug that made nothing happen. ** 11/21/2001 - Put bug back in because to much was happening. *************************************************************/
Filed under: Microsoft SQL Server, SQLServerPedia Syndication, T-SQL Tagged: code language, comments, language sql, microsoft sql server, sql statements, T-SQL