Oracle:我如何反省视图?

时间:2015-03-16 18:49:02

标签: oracle view introspection

如何在Oracle中内省视图?例如,如果我有

create view v as select foo, bar from baz

我想知道foobar是第一和第二列。

1 个答案:

答案 0 :(得分:3)

您也可以将all_tab_columns用于视图。因此,前两列视图v将类似于:

SELECT *
  FROM all_tab_columns
 WHERE TABLE_NAME = 'V' AND column_id IN (1, 2);

然后你可以要求特定的栏目:

 SELECT COUNT(*) TOTAL
  FROM all_tab_columns
 WHERE TABLE_NAME = 'V' AND (table_name,column_id) IN (('FOO',1), ('BAR',2));

如果total为2则表示foobar是第一列和第二列。您可以通过案例或解码使其更具可读性:

SELECT CASE WHEN TOTAL = 2 THEN 1 ELSE 0 END RESULT FROM
(SELECT COUNT(*) TOTAL
      FROM all_tab_columns
     WHERE TABLE_NAME = 'V' AND (table_name,column_id) IN (('FOO',1), ('BAR',2)));