Oracle view: как получить определение столбца - PullRequest
0 голосов
/ 18 мая 2018

У меня есть представление 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')

Большое спасибо

Ответы [ 2 ]

0 голосов
/ 22 мая 2018
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;
0 голосов
/ 20 мая 2018
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;
...