在postgres / postgresql / psql中注释字符/字符?

问题描述 投票:39回答:4

postgres中评论的特征是什么?

SELECT * FROM my_table     # pound  sign produces a syntax error

谢谢你cababunga,以下似乎工作:

SELECT * FROM my_table     -- this is my comment

但这不起作用:

\dt jvcurve_thin.jvcurve_results    --  my comment #2

\ dt:额外的参数“ - ”被忽略了

postgresql
4个回答
59
投票

根据PostgreSQL文档,有内联和块样式注释。

内联样式:

SELECT 23 AS test  -- this is just a test

块样式:

/* The following is a very
 * non-trivial SQL code */
SELECT 42 AS result

18
投票

在SQL中,注释以--开头。


3
投票

它看起来不像psql在其--特定的“斜线命令”中支持传统的行尾psql评论。

但是,如果您对执行时显示的行尾注释感到满意,那么使用\echo似乎是一种有效的解决方法。例如:

\dt jvcurve_thin.jvcurve_results   \echo my comment #2

“双斜杠”分隔符元命令看起来像另一种可能性(并且没有回声的副作用)。用它开始一个新命令并立即启动--评论:

\dt jvcurve_thin.jvcurve_results   \\ -- my comment #2

最后,切换到shell并添加shell注释似乎是另一种可能性:

\dt jvcurve_thin.jvcurve_results   \! # my comment #2

2
投票

从官方文档:PostgreSQL Comments

注释是一系列以双破折号开头并延伸到行尾的字符,例如:

-- This is a standard SQL comment

或者,可以使用C风格的块注释:

/* multiline comment  * with nesting: /* nested block comment */  */

注释以/ *开头,并扩展到匹配的* /。这些块注释嵌套,如SQL标准中指定的那样,但与C不同,因此可以注释掉可能包含现有块注释的较大代码块。

在进一步的语法分析之前,注释将从输入流中删除,并有效地替换为空格。

从黑暗时代开始,它也得到了同样的支持(版本7.0)。

© www.soinside.com 2019 - 2024. All rights reserved.