Oracle根据Excel或者world数据字典自动生成建表语句和注释

      工作中一般会出现这种情况,和其他单位合作开发项目时,对方会给我们一份他们的数据字典,excl或者world的,这种情况下我们要根据数据字典将表结构建好,但是一个字段一个字段的去复制粘贴会点到手疼,而且还很容易出错,在这个情况下我自己做了一个生成建表脚本的小方法,详情如下:

1、创建生成建表语句的存储过程:

复制代码
create table t_OUTString
(
Tname        varchar2(50),
OUTString    clob,
OUTSTRING2   clob,
createddate  date
);
复制代码

 

复制代码
create or replace procedure Pro_ExToSql(SStrin in varchar2 ,TString in varchar2) is
  RString varchar2(30000);
  RowString       varchar2(30000);
  LeaveString     varchar2(30000);
  midStr          varchar2(30000);
  LeftStr         varchar2(30000);
  RightStr        varchar2(30000);
  V_Cname varchar2(100);
  V_Ctype varchar2(100);
  V_Ccomment      varchar2(500);
  a               number;
  b               number;
  i       number;
  j       number;
  insertSql       varchar2(30000);
  OUTString       varchar2(30000);
  OUTString2       varchar2(30000);
  cursor curets is
    select cname,ctype,ccomment from ets_temp order by createddate desc;
begin
  j:=1;
  select replace(SStrin,' ','#') into RString from dual;
  select instr(RString,chr(10)),substr(RString , j) into i,LeaveString from dual;
  while i>0 loop
  select substr(LeaveString , j) into LeaveString from dual;
  select instr(LeaveString,chr(10)) into i from dual;
  if i is null 
    then
      goto next;
      end if;
  select substr(LeaveString , 1 ,i ) into RowString from dual;
  j:=i+1;
  select instr(RowString,'#',1,1) ,instr(RowString,'#',1,2) into A,b from dual;
  select substr(RowString,a+1,b-a-1) ,substr(RowString,1,a-1),substr(RowString,b+1) into midStr,LeftStr,RightStr 
  from dual;
  insertSql:='insert into ETS_temp(CComment,Cname,Ctype,createddate) values('''||midStr||''','''||LeftStr||''','''||RightStr||''',sysdate)';
  Execute IMMEDIATE insertSql;
  commit;
  end loop;
  <<next>>
  null;
  open curets;
  loop
    fetch curets
      into V_Cname, V_Ctype,V_Ccomment;
    exit when curets %NOTFOUND;
      OUTString:=''||OUTString||'COMMENT ON COLUMN '||TString||'.'||V_Cname||' is '''||V_Ccomment||''';'||chr(10)||'';
      outstring2:=''||outstring2||''||V_Cname||'   '||V_Ctype||','||chr(10)||'';
      
    end loop ;
    close curets;
    outstring2:='create table '||TString||''||chr(10)||'('||outstring2||');';
    select replace(replace(outstring2,''||chr(10)||',',','),','||chr(10)||');',');') into outstring2 from dual;
  insert into t_OUTString(Tname,OUTString,OUTSTRING2,createddate) values(Tstring,OUTString,OUTSTRING2,sysdate);
  commit;
  delete from ETS_temp;
  commit;
 EXCEPTION
  WHEN OTHERS THEN
    begin
      delete from ETS_temp;
      commit; 
      end;
end Pro_ExToSql;
复制代码

2、执行过程会将建表语句insert到t_outstring表中outstring为创建注释语句,outstring2为建表语句:

复制代码
call pro_extosql('JGXLID 监管线路ID number
XLMC 线路名称 varchar2(50)
DDFS 调度方式 varchar2(10)
JHZLC 计划总里程 number(10,3)
JHYYLC 计划营运里程 number(10,3)
KSRQ 开始日期 datetime
ZJZX 周几执行 varchar2(15)
JHPCS 计划配车数 number
JHBCS 计划班次数 number
JHZGFPCS 计划早高峰配车数 number
JHWGFPCS 计划晚高峰配车数 number
JHZGFBCS 计划早高峰班次数 number
JHWGFBCS 计划晚高峰班次数 number
SZMC 首站名称 varchar2(50)
MZMC 末站名称 varchar2(50)
SZSB 首站首班 varchar2(5)
SZMB 首站末班 varchar2(5)
','TableName')
复制代码

3、在T_outstring表中取得以下信息:

复制代码
--outstring字段
COMMENT ON COLUMN TableName.JGXLID is '监管线路ID';
COMMENT ON COLUMN TableName.SZMB is '首站末班';
COMMENT ON COLUMN TableName.DDFS is '调度方式';
COMMENT ON COLUMN TableName.JHZLC is '计划总里程';
COMMENT ON COLUMN TableName.JHYYLC is '计划营运里程';
COMMENT ON COLUMN TableName.KSRQ is '开始日期';
COMMENT ON COLUMN TableName.ZJZX is '周几执行';
COMMENT ON COLUMN TableName.JHPCS is '计划配车数';
COMMENT ON COLUMN TableName.JHBCS is '计划班次数';
COMMENT ON COLUMN TableName.JHZGFPCS is '计划早高峰配车数';
COMMENT ON COLUMN TableName.JHWGFPCS is '计划晚高峰配车数';
COMMENT ON COLUMN TableName.JHZGFBCS is '计划早高峰班次数';
COMMENT ON COLUMN TableName.JHWGFBCS is '计划晚高峰班次数';
COMMENT ON COLUMN TableName.SZMC is '首站名称';
COMMENT ON COLUMN TableName.MZMC is '末站名称';
COMMENT ON COLUMN TableName.SZSB is '首站首班';
COMMENT ON COLUMN TableName.XLMC is '线路名称';

--outstring2字段
create table TableName
(JGXLID   number,
SZMB   varchar2(5),
DDFS   varchar2(10),
JHZLC   number(10,3),
JHYYLC   number(10,3),
KSRQ   datetime,
ZJZX   varchar2(15),
JHPCS   number,
JHBCS   number,
JHZGFPCS   number,
JHWGFPCS   number,
JHZGFBCS   number,
JHWGFBCS   number,
SZMC   varchar2(50),
MZMC   varchar2(50),
SZSB   varchar2(5),
XLMC   varchar2(50));
复制代码

   PS:这样就可以直接将SQL提取出来执行建表了!有更好的办法的希望大家在评论区留言,一起探讨!

你可能感兴趣的:(Oracle根据Excel或者world数据字典自动生成建表语句和注释)