Comments

Comments provide user-documented information about SQL statements, statement blocks, and stored procedures.

Syntax

/* text_of_comment */

Or

-- text_of_comment

where

text_of_comment
Represents a statement or section that should not be evaluated by the server. Comments can be inserted on a separate line, nested (-- only) at the end of a Transact-SQL command line, or within a Transact-SQL statement.

Remarks

Multiple-line comments are allowed as long as they are surrounded by /* and */. A stylistic convention often used for multiple-line comments is to begin the first line with /*, subsequent lines with **, and end with */.

Single-line or nested comments are supported with the ANSI-standard comments of the double hyphens (--). Comments inserted with -- are delimited by the end-of-line (NEWLINE) character.

There is no maximum length for comments.

Note To avoid generating an error message, do not include a GO command within a comment.

Example

Combining Comments

In this example, comments are used to document and test the behavior during different phases of development for a trigger. In this "in progress" example, parts of the trigger are commented out to narrow down problems and test only one of the conditions. Both styles of comments are used; ANSI-style comments are shown both alone and nested.

CREATE TRIGGER employee_insupd
/*
    Because CHECK constraints can only reference the column(s)
    on which the column- or table-level constraint has 
    been defined, any cross-table constraints (in this case, 
    business rules) need to be defined as triggers.

    Employee job_lvls (on which salaries are based) should be within 
    the range defined for their job. To get the appropriate range, 
    the jobs table needs to be referenced. This trigger will be 
    invoked for INSERT and UPDATES only. For information about 
    triggers, see the Microsoft SQL Server Transact-SQL Reference.
*/
ON employee
FOR INSERT, UPDATE
AS
/* Get the range of level for this job type from the jobs table. */
DECLARE @min_lvl tinyint,        -- Minimum level var. declaration
    @max_lvl tinyint,            -- Maximum level var. declaration
    @emp_lvl tinyint,            -- Employee level var. declaration
    @job_id smallint                -- Job ID var. declaration
SELECT @min_lvl = min_lvl,        -- Set the minimum level
    @max_lvl = max_lvl,             -- Set the maximum level
    @emp_lvl = i.job_lvl,        -- Set the proposed employee level
    @job_id = i.job_id            -- Set the Job ID for comparison
FROM employee e, jobs j, inserted i 
WHERE e.emp_id = i.emp_id AND i.job_id = j.job_id
IF (@job_id = 1) and (@emp_lvl <> 10) 
BEGIN
    RAISERROR ('Job id 1 expects the default level of 10.',16,-1)
    ROLLBACK TRANSACTION
END
/* Only want to test first condition. Remaining ELSE is commented out.
-- Comments within this section are unaffected by this commenting style.
ELSE
IF NOT (@emp_lvl BETWEEN @min_lvl AND @max_lvl) -- Check valid range
BEGIN
    RAISERROR ('The level for job_id:%d should be between %d and %d.',
        16, -1, @job_id, @min_lvl, @max_lvl)
    ROLLBACK TRANSACTION
END
*/
go