Generate table statements and comments based on excel tables

Application scenario: Sometimes it is necessary to provide the data of the requested table according to the table structure provided by the third-party data requester. Only excel sheet is given when provided. Such as:

 

Solutions:

1, excel vb encoding, export.

2. Baidu took a look and provided another way of thinking, writing a stored procedure to achieve it.

Create a new table, write the table structure description into the table, write the process, group by each table, describe the fields of the variable table, and do character splicing.

Store into the clob field.

 

My own implementation example:

Create table:

create table wanda_midtable_col(
   xh number,
   tablename varchar2(20),
   colname varchar2(20),
   colcomment varchar2 (500),
   coltype varchar2(100),
   isnull varchar2(10),
   isprimarykey varchar2(10),
   codetype varchar(500),
   coldesc varchar2(500),
   writedesc varchar2(500),
   bz varchar2(500)
);
create table wanda_OUTString
(
Tname        varchar2(50),
OUTString clob, --create table statement
OUTSTRING2 clob, -- comment statement
createddate  date
);

 Writing process:

create or replace procedure pro_exltosql_mintables is
  OUTString       varchar2(30000);
  OUTString2       varchar2(30000);
  v_tabledesc     varchar2(500);
  v_index         number(10);
  cursor c_tables is
    select distinct a.tablename from wanda_midtable_col a;
  cursor c_tcolms(pi_tname varchar2) is
    select * from wanda_midtable_col a where a.tablename=pi_tname order by a.xh;
begin
  begin
    for v_tname in c_tables loop
      OUTString := '';
      outstring2 := '';
      v_tabledesc := '';
      for v_tcolms in c_tcolms(v_tname.tablename) loop
        OUTString :=OUTString||'COMMENT ON COLUMN '||v_tcolms.tablename||'.'||v_tcolms.colname||' is '''||v_tcolms.colcomment||''';'||chr(10);
        select outstring2||' '||v_tcolms.colname||'   '||v_tcolms.coltype||' '||decode(upper(v_tcolms.isnull),'N','NOT NULL','') ||','||chr(10)
          into outstring2
          from dual;
        if upper(v_tcolms.isprimarykey) = 'Y' then
          null;
        end if;
      end loop;
      --Get the table description of the intermediate table
      select a.table_desc
        into v_tabledesc
        from wanda_sjqx_table a
       where a.table_name = v_tname.tablename;
      OUTString := 'COMMENT ON TABLE '||v_tname.tablename||' is '''||v_tabledesc||''';'||chr(10)||OUTString;
      outstring2:='create table '||v_tname.tablename||''||chr(10)||'('||outstring2||');';
      --create table remove the last comma
      -- Subscript of last comma
      select instr(outstring2,',',-1,1) into v_index from dual;
      outstring2:= substr(outstring2,1,v_index-1)||substr(outstring2,v_index+1,length(outstring2)-v_index);
      insert into wanda_OUTString(Tname,OUTString,OUTSTRING2,createddate)
      values(v_tname.tablename,OUTString,OUTSTRING2,sysdate);
      commit;
    end loop;
  exception
    when others then
      null;
  end;
end pro_exltosql_mintables;

 

get:

COMMENT ON TABLE IC08 is 'Historical file audit information';
COMMENT ON COLUMN IC08.AAZ261 is 'History file audit ID';
COMMENT ON COLUMN IC08.AAE140 is 'Insurance type';
COMMENT ON COLUMN IC08.AAC001 is 'person ID';
COMMENT ON COLUMN IC08.AAB001 is 'Unit ID';
COMMENT ON COLUMN IC08.AAC058 is 'document type';
COMMENT ON COLUMN IC08.AAC147 is 'document number';
COMMENT ON COLUMN IC08.AAC003 is '姓名';
COMMENT ON COLUMN IC08.AAC004 is 'gender';
COMMENT ON COLUMN IC08.AAC006 is 'Date of Birth';
....

 

create table IC08
( AAZ261   NUMBER(16) NOT NULL,
 AAE140   VARCHAR2(6) NOT NULL,
 AAC001   NUMBER(20) NOT NULL,
 AAB001   NUMBER(20) ,
 AAC058   VARCHAR2(3) NOT NULL,
 AAC147   VARCHAR2(20) NOT NULL,
 AAC003   VARCHAR2(50) NOT NULL,
 AAC004   VARCHAR2(1) NOT NULL,
 AAC006   DATE NOT NULL,
 AAC005   VARCHAR2(3) ,
 AAC024   VARCHAR2(3) ,
....

 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326392614&siteId=291194637