/*...*/(注释)(Transact-SQL)

表示用户提供的文本。服务器不计位于 /* 和 */ 之间的文本。

主题链接图标Transact-SQL 语法约定

语法

/*
text_of_comment
*/

参数

  • text_of_comment
    注释文本。它是一个或多个字符串。

注释

注释可以插入单独行中,也可以插入 Transact-SQL 语句中。多行的注释必须用 /* 和 */ 指明。用于多行注释的样式规则是,第一行用 /* 开始,接下来的注释行用 ** 开始,并且用 */ 结束注释。

注释没有最大长度限制。

支持嵌套注释。如果在现有注释内的任意位置上出现 /* 字符模式,便会将其视为嵌套注释的开始,因此,需要使用注释的结尾标记 */。如果没有注释的结尾标记,便会生成错误。

例如,以下代码生成一个错误。

DECLARE @comment AS varchar(20);
GO
/*
SELECT @comment = '/*';
*/ 
SELECT @@VERSION;
GO 

若要解决该错误,请执行以下更改。

DECLARE @comment AS varchar(20);
GO
/*
SELECT @comment = '/*';
*/ */
SELECT @@VERSION;
GO 

示例

以下示例使用注释解释将要执行哪个代码段。

USE AdventureWorks2008R2;
GO
/*
This section of the code joins the Person table with the Address table, 
by using the Employee and BusinessEntityAddress tables in the middle to 
get a list of all the employees in the AdventureWorks2008R2 database 
and their contact information.
*/
SELECT p.FirstName, p.LastName, a.AddressLine1, a.AddressLine2, a.City, a.PostalCode
FROM Person.Person AS p
JOIN HumanResources.Employee AS e ON p.BusinessEntityID = e.BusinessEntityID 
JOIN Person.BusinessEntityAddress AS ea ON e.BusinessEntityID = ea.BusinessEntityID
JOIN Person.Address AS a ON ea.AddressID = a.AddressID;
GO