Comments in T-SQL SQL Server 2008 2012 with examples


Examples of Comments in T-SQL SQL Server 2008 2012 

  1. In this article i will explain the types of comments in SQL Server Programming in SQL Server 20012 or 2008
  2. Basically there are two types of commenting syntax that are widely used in SQL Programming which are described as follows
  • Block Comments
  • In-Line Comments
let us now see each comment pattern with examples
Block Comments
  1. Block comments are typically used at the beginning of a T-SQL code to provide an explanation about the purpose of the code.
  2. The following example contains a block comment that describes the purpose of the T-SQL code. Comments are enclosed within the /* and */ elements.
Example:
/* The purpose of the Transact-SQL 
statement below is to retrieve the 
Firstname and Lastname of StudentID 1 */

USE Adventureworks
GO
SELECT Firstname, Lastname
FROM students
WHERE StudentID = 1


In-Line Comments
  1. In-line comments are added within the T-SQL code to add descriptive comments to portions of the code.
  2. In-line comments begin with -- (two hyphens). Any characters to the right of this are commented out on the line where the -- are placed, and are excluded from the T-SQL statement. Comments inserted with -- are terminated by the newline character.
  3. Additional elements can also be used to add flexibility and logic to any T-SQL statements. These elements can add dynamic capabilities to your statements and logic, and provide you with the ability to create powerful T-SQL statements
  4. The following is an example of an in-line comment within a T-SQL statement.
Example:
USE Adventureworks
GO
SELECT Firstname, Lastname 
FROM students
WHERE StudentID = 1 -- Only StudentID one is returned

0 comments:

Post a Comment