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

SQL Server has two different ways to comment code.

SQL SERVER - Two Different Ways to Comment Code - Explanation and Example comment

1) Line comments

Solarwinds

Line comments start with two dashes (–) and ends with carriage returns. Everything between two dashes(–) and carriage returns is considered as comment. This comment 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 *
FROM Sales.Products --This table name
WHERE ProductID > 10 --This is a filter condition

2) Block comments 

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

/*This is an example of blog comment
Following table is an 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 commenting text.

Reference : Pinal Dave (https://blog.sqlauthority.com)

Solarwinds
, ,
Previous Post
SQLAuthority News – Book Review – SQL Server 2005 Practical Troubleshooting: The Database Engine
Next Post
SQLAuthority News – Microsoft SQL Server Compact 3.5 Server Tools Beta 2 Released

Related Posts

10 Comments. Leave new

  • 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?

    Reply
  • 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??

    Reply
  • Imran Mohammed
    July 19, 2009 10:35 pm

    @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.

    Reply
  • use of nested comments, where to use

    Reply
  • Hey can anyone help me in using loops to get some values from a database with stored procedure in SQL SERVER

    Reply
  • 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

    Reply
  • Thank you for your suggestions and hints. I have been searching for detailed explanations at creating my own blog. Thank you!

    Reply
  • Nice!

    Reply

Leave a Reply

Menu