0

all_col_commentsテーブルに、このテーブルの列に対するテーブルとコメントがあります。テーブルの列名の代わりに、選択クエリの all_col_comments から列コメントを出力するにはどうすればよいですか?

例:

SELECT column1 AS 'comment for column1 from all_col_comments table'
     , column2 AS 'comment for column2 from all_col_comments table',
  FROM my_table;

質問 V2やりたいこと

SQL> create table myTable(
  2    id           NUMBER(2),
  3    value        NUMBER(6,2)
  4  )
  5  /
Table created.

SQL>
SQL> -- prepare data
SQL> insert into myTable(ID,  value)values (1,9)
  2  /

1 row created.

SQL> insert into myTable(ID,  value)values (2,2.11)
  2  /

1 row created.

SQL>
SQL> select * from myTable
  2  /

        ID      VALUE
---------- ----------
         1          9
         2       2.11

2 rows selected.

SQL>
SQL> COMMENT ON COLUMN myTable.id IS
  2  'id stores the id';

Comment created.

SQL> COMMENT ON COLUMN myTable.value IS
  2  'value stores the some comment for value';

Comment created.

次に、いくつかの選択を行ってこれを取得する必要があります

SQL>
SQL> select ??? ...

id stores the id      value stores the some comment for value
      ----------                                   ----------
               1                                            9
               2                                         2.11

    2 rows selected.
4

3 に答える 3

2

あなたが求めていることを動的SQLなしで行う方法はないと思います。列のエイリアス ( の後のビットAS) をパラメーター化する方法はないため、連結を使用して SQL 文字列を構築する必要があります。

連結を使用して SQL 文字列を構築しているため、SQL インジェクションを軽減する方法を考える必要があります。(リスクは低いでしょう。データベース テーブルのコメントを変更する権限を持っている場合は、適切に悪意のある列のコメントを作成する必要なく、データベースに直接アクセスするのに十分な権限を持っている可能性があります。) 少なくとも、 SQL文字列は列のエイリアスを"文字で囲む必要があり('質問のように)、列のコメントをSQL文字列に連結する前に、コメントに"文字が含まれていないことを確認する必要があります。

編集: あなたのコメントから、Perl を使用してデータベースと通信しているようです。以下は未テストのスケッチで、うまくいけば SQL 文字列を構築する方法を示しています。ギャップを埋めるのはあなたに任せます:

# The list of columns to select from the table.
# Must exactly match the names of columns returned from user_col_comments
my @columns = qw/COLUMN1 COLUMN2 COLUMN3 COLUMN4/;

# Are we adding the first column?
my $first = 1;

my $sql = 'SELECT ';

# Hash that maps column names to comments.
my %commenthash = ();

# TODO query user_col_comments and add to %commenthash the column names and comments.

for my $col (@columns) {

    my $comment = $commenthash{$col};

    unless (defined $comment) {
        # Use the column name if no comment was found.
        $comment = $col;
    }

    if ($comment =~ /"/) {
        die "can't use double-quotes in column comment: $comment";
    }

    # Add separating comma if this is not the first column.
    $sql .= ', ' if !$first;

    $sql .= qq!"$col" AS "$comment"!;

    $first = 0;
}

$sql .= ' FROM my_table';

# TODO execute SQL.

データベースに対して 2 回クエリを実行する必要があることに注意してください。1 回目は列のコメントを取得するため、もう 1 回は実際のデータを取得するためです。

于 2013-03-16T22:00:53.903 に答える
0

user_col_comments を使用して、列のコメントを取得できます

SELECT *
FROM user_col_comments
WHERE table_name = 'MYTABLE';
于 2013-03-16T16:34:58.533 に答える
-1

動的クエリの使用 (未テスト):

begin
 s := ' select ';
 for i in ( select column_name, column_comment from user_tables where table_name = 'MyTable' )
 loop
  s := i.column_name ||' AS ' ||'"'|| i.column_comment || '", '; 
 end loop;
s := s || ' from ' || 'MyTable';

dbms_output.put_line( 'SQL: ' || s ); -- SHOW
execute immediate ... -- execute
end;
/
于 2013-03-16T17:43:22.870 に答える