SQL SERVER – Two Different Ways to Comment Code – Explanation and Example

SQL Server has two different ways to comment code.

1) Line comments

Line comments starts with two dashes(–) and ends with carriage returns. Everything between two dashes(–) and carriage returns is considered as comment. This comments can be placed anywhere in the code. This comment can be placed after code in the same line and everything before two dashes (–) will execute like normal T-SQL and after two dashes(–) will not be executed and considered as comment.

Example:
SELECT *
–---This table name
FROM Sales.Products
WHERE ProductID > 10 –---This is filter condition

2) Block comments

Block comments starts with a forward-slash and asterisk (/*) and ends with asterisk and forward-slash (*/). Everything between forward-slash and asterisk (/*) and ends with asterisk and forward-slash (*/) is considered as comments. This comment are usually placed before or after big chunk of code, however it can be placed anywhere in the code. This comment can span more than one line.

Example:

/*This is example of blog comment
Following table is example of same retrieve
and filter condition*/
SELECT *
FROM Sales.Products
WHERE ProductID > 10

Additional Notes:
There is no limit for the length of the comment.
Comments can be nested.
GO Command is not allowed anywhere in commented text.

Reference : Pinal Dave (http://blog.SQLAuthority.com)

About these ads

10 thoughts on “SQL SERVER – Two Different Ways to Comment Code – Explanation and Example

  1. Does the SQL Server 2005 Management Studio tool interpret the TSQL differently than the SQL Server 2000 Query Analyzer tool? The script contains a block comment like:

    /*
    (TSQL code)
    GO
    (more TSQL code)
    */
    (even more TSQL code)

    Running this script in Query Analyzer connected to a SQL Server 2000 server generates an error. Running this script in Managment Studio connected to a SQL Server 2000 server does not. Both tools were connecting to the same SQL Server 2000 serve. So are both client tools doing some interpretation before sending the code to the server?

  2. Hi,

    How to add comments to my table for further reference?

    As we aware, in oracle there is a provision to add comments to a table.

    COMMENT ON TABLE Employee IS ‘This is a table for Employee.';

    Is there any way to add comments in SQL Sevrer??

  3. @Praveen

    You can add comment/Description for a table/column in 2 ways.
    1. Through Scripts
    2. Through User Interface (UI).

    1. Through Scripts:

    –This is an Example given in Adventure Works Database.

    –This is to add a comment to a table
    — Table Name : Department, Schema Name: HumanResources.
    — Table Description/Comment is : Lookup table containing the departments within the Adventure Works Cycles company.

    EXEC sys.sp_addextendedproperty
    @name=N’MS_Description’, @value=N’Lookup table containing the departments within the Adventure Works Cycles company.’ ,
    @level0type=N’SCHEMA’,@level0name=N’HumanResources’,
    @level1type=N’TABLE’,@level1name=N’Department’

    — This is to add a comment to one column
    — Column Name: ModifiedDate
    — Table Name : Department.
    — Schema Name : HumanResource.
    –Column Description/Comment: Date and time the record was last updated.

    EXEC sys.sp_addextendedproperty
    @name=N’MS_Description’, @value=N’Date and time the record was last updated.’ ,
    @level0type=N’SCHEMA’,@level0name=N’HumanResources’,
    @level1type=N’TABLE’,@level1name=N’Department’,
    @level2type=N’COLUMN’,@level2name=N’ModifiedDate’

    2: Through UI.
    Step1 : Log IN to SQL Server Management Studio.
    Step2: Expand Server Name
    Step3: Expand Databases
    Step4: Expand Database Name
    Step5: Expand Tables

    To Add Comment to Table
    Step6: Right Click Table Name – Click Properties.
    Step7: Click Extended Properties.
    Step8: To Your right side, Under Name: Write any name, Lets Say MS_Description or any other name you want.
    Step9: Click On Browse Button, A pop box will show up.
    Step10: Type comment in that box, Click Ok.

    To Add Comment to column,
    Step6(a) Expand Table Name
    Step7(a) Expand Columns
    Step8(a) Right Click on one Column – Click Properties
    Follow steps 7,8, 9 and 10.

    ~ IM.

  4. hi thanx a lot i have learned more and more from ur web sit and ur youtube videos bout i have a small request what will be if i want the product id in the following example equal max not a certain value(10 in the below example) plz replay me via mail

    SELECT *
    FROM Sales.Products
    WHERE ProductID > 10
    as the above example

  5. Pingback: SQL SERVER – Tricks to Comment T-SQL in SSMS – SQL in Sixty Seconds #019 – Video « SQL Server Journey with SQL Authority

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