向 SQL 语句添加注释

适用于:check marked yes Databricks SQL check marked yes Databricks Runtime

注释可用于记录 SQL 代码和暂时禁用 SQL 代码。

可以在语句之前、之后和语句内向 SQL 代码添加注释。 除非系统将注释识别为提示,否则 Azure Databricks 将忽略注释。

支持以下形式的注释:

简单注释

简单注释用于覆盖整个文本行或以 -- 开头的文本行的其余部分

语法

-- text

参数

  • text:排除行尾 (EOL) 字符的任何文本,例如 \n

示例

> -- This is a comment

> SELECT 1; -- This is also a comment
  1

> SELECT -- This is a comment
 1;
  1

> SELECT -- Comments are not limited to Latin characters: 评论 😊
 1;
  1

> SELECT '-- This is not a comment';
  -- This is not a comment

> SELECT -- This is a bad comment because the "one" should be on the next line... 1
 Syntax error

> SELECT -- this is a bad
comment because it contains an EOL character
  1;
 Syntax error

括号注释

带括号的注释用于覆盖多行文本或文本行的一部分。

语法

bracketed_comment
  /* text [ bracketed_comment [...] ] text */

参数

  • text:任何文本,包括行尾字符 (EOL) 字符,不包括 /**/

示例

> /* This is a comment */

> SELECT 1; /* This is also a comment */

> SELECT /* This is a comment
  that spans multiple lines */ 1;

> SELECT /* Comments are not limited to Latin characters: 评论 😊 */ 1;

> SELECT /* Comments /* can be */ nested */ 1;

> SELECT /* Quotes in '/*' comments "/*" are not special */ */ */ 1;

> /* A prefixed comment */ SELECT 1;

> SELECT '/* This is not a comment */';
  /* This is not a comment */