Pages

Wednesday, October 24, 2018

Find table/view name & ddl from sql copybook


This works for PS cobol copybooks, assuming its formatting style, text sacn/grab change needed if not the same format.

1. get rec name


cut -c7- xxx.sql|  grep -v ^\* | sed  's/\-\-.*$//' | sed -n '/FROM/I,/WHERE/Ip'  | grep -iv WHERE | awk -F"[(), ]" 'BEGIN{IGNORECASE=1} {for(i = 1; i <= NF; i++) if (match ($i,"^PS")) print $i}'|sort|uniq


2. get ddl:

define obj=&1

declare 
   buf clob;
   typ varchar2(30);

begin 


   dbms_output.ENABLE(200000);

   select OBJECT_TYPE into typ from user_objects where object_name = upper('&obj') and OBJECT_TYPE in ('TABLE','VIEW');   

   select DBMS_METADATA.GET_DDL(typ,upper('&obj')) into buf from dual;
   

   DBMS_OUTPUT.PUT_LINE(buf);

 exception
         WHEN OTHERS then
           declare
              errcd  NUMBER := SQLCODE;
              errmsg VARCHAR2(300) := SQLERRM;
           begin
              DBMS_OUTPUT.PUT_LINE(errcd ||','||errmsg);
           end;

end;
/