【问题标题】:Oracle view: how to obtain a column definitionOracle 视图:如何获取列定义
【发布时间】:2025-12-23 23:45:16
【问题描述】:

我有一个 Oracle 视图:

create view schemaName.viewName as
select case when 1=1 then 1 else 2 end as col1, decode('A','A','B','C') as col2 from dual

有没有办法获得包含这些信息的输出或表格:

Column_Name: Col1
Column_Definition: case when 1=1 then 1 else 2 end

Column_Name: Col2
Column_Definition: decode('A','A','B','C')

非常感谢

【问题讨论】:

  • 我认为没有办法做到这一点。
  • Oracle 可以告诉你视图的列名和数据类型,但是没有列的定义。这将很难实现,因为支持视图的查询中可能没有简单的列定义。

标签: oracle view definition


【解决方案1】:
Here is the below anonymous block to get column name and column definition for above view.

      DECLARE

      a varchar2(1000);
      b varchar2(1000);
      c varchar2(1000);

      BEGIN

      EXECUTE  IMMEDIATE 'SELECT TEXT FROM user_views
      WHERE VIEW_NAME=''TEST_VW'' ' INTO a;

      SELECT  regexp_replace(regexp_substr(a, '^[^,]*'),'select|as\W','') -- getting first argument case to get "case when 1=1 then 1 else 2 end col1"

      into b from dual;

      DBMS_OUTPUT.PUT_LINE( 'Column_Name: '|| regexp_substr(b,'[^ ]+$')); -- this command to get last col1 from "case when 1=1 then 1 else 2 end col1"

      DBMS_OUTPUT.PUT_LINE('Column_Definition: '|| regexp_replace(b,'[^ ]+$','')); -- this command to make col1 to empty to get column definition

      select  regexp_replace(substr(a,length(regexp_substr(a, '^[^,]*'))+2),'select|dual|from|as\W','') into c from dual; -- this command to get 2nd column "decode('A','A','B','C') col2"


      DBMS_OUTPUT.PUT_LINE( 'Column_Name: '||  REGEXP_SUBSTR ( c , '[^ ]+' , 1 , 2 )); -- This command will get 2nd column col2 from "decode('A','A','B','C') col2"

      DBMS_OUTPUT.PUT_LINE( 'Column_Definition: '||  REGEXP_SUBSTR ( c , '[^ ]+' , 1 , 1 ));  -- This command will get 1st column decode('A','A','B','C') from "decode('A','A','B','C') col2"

      END;

【讨论】:

  • 我的查询只是一个示例,对于每个可能的 oracle 视图我都需要它,并且没有固定的列。谢谢
【解决方案2】:
Thanks for clarifying ... just try below block and see if this helps ..Also let me know for any column definition which is incorrect

prerequiste for ananymous block:

create view TEST_VW as
select case when 1=1 then 1 else 2 end as col1, decode('A','A','B','C') as col2, 
null as col3 , sysdate as col4 , 'var1' as col5  
from dual;

-- 在视图定义中,我希望每列的格式和每列的分隔符是分隔此块的列名和定义的关键。

 create sequence seq
      start with 1;

create table col_nm_def 
      (column_nm varchar2(1000),
      column_def varchar2(1000),
      id number);


Ananymous block;

DECLARE 

      a varchar2(1000); 
      b varchar2(1000); 
      c varchar2(1000);
      d number;

      BEGIN 

      EXECUTE  IMMEDIATE 'SELECT TEXT FROM user_views 
      WHERE VIEW_NAME=''TEST_VW'' ' INTO a;

       EXECUTE  IMMEDIATE 'delete from col_nm_def';

       EXECUTE  IMMEDIATE  'drop sequence seq';


       EXECUTE  IMMEDIATE     'create sequence seq
      start with 1';


      select regexp_replace(regexp_replace(a,'select|from|dual',''),'as\W|, ' , '|') into b from dual; -- replacing as and (, ) to | symbol (try to change symbol if view uses this pipe in transformation)

      select regexp_count(b,'[|]') into d from dual; 

      d:=d+1;

      For i in 1..d

      LOOP

      insert into col_nm_def  values (REGEXP_SUBSTR ( b , '[^|]+' , i+1 , i+1 ),REGEXP_SUBSTR ( b , '[^|]+' , i , i ),seq.nextval);
      COMMIT;

    END LOOP;
    END;


Final result : select only odd rows 

select * from col_nm_def where mod(id,2)=1 order by ID;

【讨论】: