Oracle SQL添加多行表注释或列注释

10

我希望能够添加多行的表格/列注释。

通常情况下,这是这样使用的:

COMMENT ON TABLE USERS IS 'User table has the user data'

我需要的是一种方法,在单引号内插入换行符,就像这样;

COMMENT ON TABLE USERS IS 'User table has the user data <smthg_here_for_new_line> 1- Name column has name <smthg_here_for_new_line> 2- Number Column has the id'

这样表格评论将会显示为:

User table has the user data
1- Name column has name
2- Number Column has the id

有人知道如何添加多行表/列注释吗?

2个回答

6

你可以在注释声明的单引号内简单地插入换行符,例如:

COMMENT ON COLUMN MYTABLE.MYCOLUMN
IS
'Line 1
Line 2.
Line 3';

需要注意的是,在 SQL Developer(以及可能其他工具)中,这种情况不总是按预期显示。使用以下查询语句...

SELECT *
FROM USER_COL_COMMENTS
WHERE
  TABLE_NAME = 'MYTABLE'
  AND COMMENTS IS NOT NULL;

如果您在脚本输出中找到了想要的内容(例如,强调查询语句、右键单击并选择“运行脚本”),那么你会得到确切的结果。

TABLE_NAME COLUMN_NAME COMMENTS                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
---------- ----------- --------------
MYTABLE    MYCOLUMN    Line 1
                       Line 2
                       Line 3
MYTABLE    OTHERCOLUMN Other comments

但在查询结果中(即,突出显示查询,右键单击,选择“运行语句”),或者在打开表并查看“列”选项卡时,完整的注释将在一行上连续运行。

注意:可以查询这些注释的表为:

  • 表上的注释:USER_TAB_COMMENTS
  • 列上的注释:USER_COL_COMMENTS

0
在SQLPlus中,您可以使用chr(10)与concat(或在Microsoft Windows环境中使用chr(13) || chr(10)):
'User table has the user data' || chr(10) || '1- Name column has name...'

另外,通过将SQLBLANKLINES设置为ON,应该可以仅对换行符进行解释:

SET SQLBLANKLINES ON
COMMENT ON TABLE USERS IS 'User table has the user data
1- Name column has name
2- Number Column has the id'

我尝试了这个操作,但是出现了以下错误。我在 TOAD 上作为单个 SQL 语句运行,错误如下:COMMENT ON TABLE USER IS 'User table comment1 here' || chr(10) || '1- User table subcomment here' Error at line 1 ORA-00933: SQL command not properly ended - Levent Divilioglu
而且,无论是chr(10)还是chr(13),都没有起作用(一开始我尝试使用chr(10),因为Oracle服务器在*nix机器上,然后我尝试使用chr(13),因为我通过运行在Windows机器上的TOAD软件进行尝试,但都没有起作用)。 - Levent Divilioglu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接