Pages

Friday, 21 June 2013

Comments in Sql Server:

Comments are nonexecuting text strings in program code (also known as remarks).

 comments to document code makes future program code maintenance easier.

Microsoft® SQL Server™ supports two types of commenting characters:

-- (Comment)

Use -- for single-line or nested comments.
Indicates user-provided text.
The comment is not evaluated by the server.
Comments inserted with -- are delimited by the newline character.
There is no maximum length for comments.
Two hyphens (--) is the SQL-92 standard indicator for comments.
Syntax
-- text_of_comment
Arguments
text_of_comment

/* ... */ (forward slash-asterisk character pairs).

Everything from the open comment pair (/*) to the close comment pair (*/) is considered part of the comment.

Multiple-line /*  */ comments cannot span a batch.

The complete comment must be contained within a batch.

For example, in SQL Query Analyzer and the osql utility, the GO command signals the end of a batch. When the utilities read the characters GO in the first two bytes of a line, they send all the code since the last GO command to the server as one batch. If a GO occurs at the start of a line between the /* and */ delimiters, then an unmatched comment delimiter will be sent with each batch and they will trigger syntax errors.
Here is some basic information regarding comments:
  • All alphanumeric characters or symbols can be used within the comment. SQL Server ignores all characters within a comment, although SQL Query Analyzer, osql, and isql will search for GO as the first two characters in lines within a multiple line comment.
  • There is no maximum length for a comment within a batch. A comment can consist of one or more lines.
Note: Including a GO command within a comment generates an error message.
Scenario 1:
go
declare @var int
set @var=10
select @var
/*
GO
*/
select @var

In 2008 it acts as commented block
Result set:
10
10
But in 2000 it acts as batch delimiter, it throws below error.
Missing end comment mark '*/'.
Line 1: Incorrect syntax near '*'.
Must declare the variable '@var'.





Scenario 2:
go
declare @var int
set @var=10
select @var
/* hi this is start of outer comment section
/*This is nested comment*/
we are closing the outer comment section */
select @var

In Sql Server 2000 the text after nested comment section is treated as commented text but it displays as general(un commented section) text.


go
declare @var int
set @var=10
select @var
/* hi this is start of outer comment section
/*This is nested comment*/
we are closing the outer comment section */
select @var

But in Sql Server 2008 the text after nested comment section is treated as commented text and also it displays as commented section.


Best Practise:To comment multiple lines use ctrl+sht+c in sql server 2000.
Thanks.. 

No comments:

Post a Comment