oracle存储过程创建表分区实例

用存储过程创建数据表:
创建时注意必须添加authid current_user,如果创建的表已存在,存储过程继续执行,但如不不加此关键语句,存储过程将出现异常,
这个语句相当于赋权限。
例1
创建语句如下:

复制代码 代码如下:

create or replace

procedure sp_create_mnl(i_id varchar2) authid current_user  as

  /*********************************

名称:sp_create_mnl

功能描述:创建模拟量历史数据存储表

修订记录:

版本号   编辑时间  编辑人  修改描述

1.0.0    2012-9-20 wylaok  1.创建此存储过程

1.0.1    2012-9-21 wylaok  2.修改表名称及变量名称,增加必要注释

入参出参描述:

i_id 测点编号

**********************************/

      v_tablename varchar2(30);–表名

      v_flag number(10,0);

      v_sqlfalg varchar(200);

begin

  v_flag:=0;

      v_tablename:=concat(‘mnl’, upper(i_id));

      v_sqlfalg:=’select count(*) from user_tables where table_name=”’||v_tablename||””;

      dbms_output.put_line(v_sqlfalg);

      execute immediate v_sqlfalg into v_flag;

      if v_flag=0 then  –如果没有这个表 则去创建

         begin

  execute immediate ‘create table ‘||v_tablename ||’

  ( datetime date,

  min00    float,

  agv00    float,

  max00    float,

  min05    float,

  avg05    float,

  max05    float,

  min10    float,

  agv10    float,

  max10    float,

  min15    float,

  agv15    float,

  max15    float,

  min20    float,

  agv20    float,

  max20    float,

  min25    float,

  agv25    float,

  max25    float,

  min30    float,

  agv30    float,

  max30    float,

  min35    float,

  agv35    float,

  max35    float,

  min40    float,

  agv40    float,

  max40    float,

  min45    float,

  agv45    float,

  max45    float,

  min50    float,

  agv50    float,

  max50    float,

  min55    float,

  agv55    float,

  max55    float,

  minv     float,

  maxv     float,

  avgv     float,

  maxtime  date,

  mintime  date

  )

  tablespace wyg

  pctfree 10

  initrans 1

  maxtrans 255

  storage

  (

    initial 512k

    next 512k

    minextents 1

    maxextents unlimited

    pctincrease 0

  )’;

  —    execute immediate sqlstr;

         end;

      end if;

      end;

调用此存储过程:


复制代码 代码如下:

begin

  createmnl(‘mnl_14’);

  end;

例2

复制代码 代码如下:

create or replace procedure bip_mms_partition_proc as

  v_mms_task_tab    varchar2(50); –表名

  v_mms_content_tab varchar2(50);

  v_mms_user_tab    varchar2(50);

  v_tablespace      varchar2(20); –表空间

  v_partpreflag     varchar2(50); –分区名标识

  v_sqlcursor       number; –游标

  v_sqlexec         varchar2(2000); –执行语句

  v_partpredate     varchar2(20); –分区日期

  v_rangevalue      number;

  v_rangedate       number;

  v_rows            number(30) := 0;

  v_num             number(30) := 0;

  verrinfo          varchar2(200);

  p_datefrom        number;

  p_partnum         number;

  p_range           number;

begin

  v_mms_task_tab    := ‘bip_mms_mt_task_log_tab_test’;

  v_mms_content_tab := ‘bip_mms_mt_content_tab_test’;

  v_mms_user_tab    := ‘bip_mms_mt_user_log_tab_test’;

  — 读取配置参数

  begin

    select to_number(value)

      into p_datefrom

      from bip_others_properties_tab

     where name = ‘p_datefrom’;

    select to_number(value)

      into p_partnum

      from bip_others_properties_tab

     where name = ‘p_partnum’;

    select to_number(value)

      into p_range

      from bip_others_properties_tab

     where name = ‘p_range’;

  exception

    when others then

      begin

        p_datefrom := 0;

        p_partnum  := 1;

        p_range    := 180;

      end;

  end;

  –记录存储过程添加分区

  insert into bip_log_stat_exec_tab

  values

    (to_char(sysdate, ‘yyyymmddhh24miss’),

     ‘bip_mms_partition_proc_add’,

     ‘begin’);

  commit;

  –add partition 

  for i in 1 .. p_partnum loop

    –bip_mms_mt_content_tab 添加分区

    v_partpredate := to_char(sysdate + p_datefrom + i, ‘yyyymmdd’);

    dbms_output.put_line(v_partpredate);

    v_num         := 0;

    v_tablespace  := ‘bip_mms_ts_test’;

    v_partpreflag := ‘mms_mt_content’;

    select count(*)

      into v_num

      from user_tab_partitions

     where table_name = v_mms_content_tab

       and substr(partition_name, 16, 8) = v_partpredate;

    if v_num < 1 then

      v_rangedate  := to_char(sysdate + p_datefrom + i, ‘yyyymmdd’);

      v_rangevalue := v_rangedate || ‘240000’;

      dbms_output.put_line(v_rangevalue);

      v_sqlexec := ‘alter table ‘ || v_mms_content_tab || ‘ add partition ‘ ||

                   v_partpreflag || ‘_’ || v_partpredate ||

                   ‘ values less than(”’ || v_rangevalue ||

                   ”’) tablespace ‘ || v_tablespace;

      dbms_output.put_line(v_sqlexec);

      v_sqlcursor := dbms_sql.open_cursor;

      dbms_sql.parse(v_sqlcursor, v_sqlexec, dbms_sql.native);

      v_rows := dbms_sql.execute(v_sqlcursor);

      dbms_sql.close_cursor(v_sqlcursor);

    end if;

    –bip_mms_mt_task_log_tab_test 添加分区

    v_partpredate := to_char(sysdate + p_datefrom + i, ‘yyyymmdd’);

    v_num         := 0;

    v_tablespace  := ‘bip_mms_ts_test’;

    v_partpreflag := ‘mms_mt_task_log’;

    select count(*)

      into v_num

      from user_tab_partitions

     where table_name = v_mms_task_tab

       and substr(partition_name, 17, 8) = v_partpredate;

    if v_num < 1 then

      v_rangedate  := to_char(sysdate + p_datefrom + i, ‘yyyymmdd’);

      v_rangevalue := v_rangedate || ‘240000’;

      v_sqlexec    := ‘alter table ‘ || v_mms_task_tab || ‘ add partition ‘ ||

                      v_partpreflag || ‘_’ || v_partpredate ||

                      ‘ values less than(”’ || v_rangevalue ||

                      ”’) tablespace ‘ || v_tablespace;

      dbms_output.put_line(v_sqlexec);

      v_sqlcursor := dbms_sql.open_cursor;

      dbms_sql.parse(v_sqlcursor, v_sqlexec, dbms_sql.native);

      v_rows := dbms_sql.execute(v_sqlcursor);

      dbms_sql.close_cursor(v_sqlcursor);

    end if;

    –bip_mms_mt_user_log_tab_test 添加分区

    v_partpredate := to_char(sysdate + p_datefrom + i, ‘yyyymmdd’);

    v_num         := 0;

    v_tablespace  := ‘bip_mms_ts_test’;

    v_partpreflag := ‘mms_mt_user_log’;

    select count(*)

      into v_num

      from user_tab_partitions

     where table_name = v_mms_user_tab

       and substr(partition_name, 17, 8) = v_partpredate;

    if v_num < 1 then

      v_rangedate  := to_char(sysdate + p_datefrom + i, ‘yyyymmdd’);

      v_rangevalue := v_rangedate || ‘240000’;

      v_sqlexec    := ‘alter table ‘ || v_mms_user_tab || ‘ add partition ‘ ||

                      v_partpreflag || ‘_’ || v_partpredate ||

                      ‘ values less than(”’ || v_rangevalue ||

                      ”’) tablespace ‘ || v_tablespace;

      dbms_output.put_line(v_sqlexec);

      v_sqlcursor := dbms_sql.open_cursor;

      dbms_sql.parse(v_sqlcursor, v_sqlexec, dbms_sql.native);

      v_rows := dbms_sql.execute(v_sqlcursor);

      dbms_sql.close_cursor(v_sqlcursor);

    end if;

  end loop;

  commit;

  insert into bip_log_stat_exec_tab

  values

    (to_char(sysdate, ‘yyyymmddhh24miss’), ‘bip_mms_partition_proc_add’, ‘end’);

  commit;

  –delete partition

  insert into bip_log_stat_exec_tab

  values

    (to_char(sysdate, ‘yyyymmddhh24miss’),

     ‘bip_mms_partition_proc_del’,

     ‘begin’);

  commit;

  begin

    v_partpreflag := ‘mms_mt_content’ || ‘_’ ||

                     to_char(sysdate – p_range, ‘yyyymmdd’);

    dbms_output.put_line(v_partpreflag);

    v_sqlexec := ‘alter table ‘ || v_mms_content_tab ||

                 ‘ truncate partition ‘ || v_partpreflag;

    dbms_output.put_line(v_sqlexec);

    v_sqlcursor := dbms_sql.open_cursor;

    dbms_sql.parse(v_sqlcursor, v_sqlexec, dbms_sql.native);

    v_rows := dbms_sql.execute(v_sqlcursor);

    dbms_sql.close_cursor(v_sqlcursor);

    dbms_output.put_line(v_partpreflag || ‘ truncated’);

    v_sqlexec := ‘alter table ‘ || v_mms_content_tab || ‘ drop partition ‘ ||

                 v_partpreflag;

    dbms_output.put_line(v_sqlexec);

    v_sqlcursor := dbms_sql.open_cursor;

    dbms_sql.parse(v_sqlcursor, v_sqlexec, dbms_sql.native);

    v_rows := dbms_sql.execute(v_sqlcursor);

    dbms_sql.close_cursor(v_sqlcursor);

    dbms_output.put_line(v_partpreflag || ‘ dropped’);

  end;

  begin

    v_partpreflag := ‘mms_mt_task_log’ || ‘_’ ||

                     to_char(sysdate – p_range, ‘yyyymmdd’);

    dbms_output.put_line(v_partpreflag);

    v_sqlexec := ‘alter table ‘ || v_mms_task_tab || ‘ truncate partition ‘ ||

                 v_partpreflag;

    dbms_output.put_line(v_sqlexec);

    v_sqlcursor := dbms_sql.open_cursor;

    dbms_sql.parse(v_sqlcursor, v_sqlexec, dbms_sql.native);

    v_rows := dbms_sql.execute(v_sqlcursor);

    dbms_sql.close_cursor(v_sqlcursor);

    dbms_output.put_line(v_partpreflag || ‘ truncated’);

    v_sqlexec   := ‘alter table ‘ || v_mms_task_tab || ‘ drop partition ‘ ||

                   v_partpreflag;

    v_sqlcursor := dbms_sql.open_cursor;

    dbms_sql.parse(v_sqlcursor, v_sqlexec, dbms_sql.native);

    v_rows := dbms_sql.execute(v_sqlcursor);

    dbms_sql.close_cursor(v_sqlcursor);

    dbms_output.put_line(v_partpreflag || ‘ dropped’);

  end;

  begin

    v_partpreflag := ‘mms_mt_user_log’ || ‘_’ ||

                     to_char(sysdate – p_range, ‘yyyymmdd’);

    dbms_output.put_line(v_partpreflag);

    v_sqlexec := ‘alter table ‘ || v_mms_user_tab || ‘ truncate partition ‘ ||

                 v_partpreflag;

    dbms_output.put_line(v_sqlexec);

    v_sqlcursor := dbms_sql.open_cursor;

    dbms_sql.parse(v_sqlcursor, v_sqlexec, dbms_sql.native);

    v_rows := dbms_sql.execute(v_sqlcursor);

    dbms_sql.close_cursor(v_sqlcursor);

    dbms_output.put_line(v_partpreflag || ‘ truncated’);

    v_sqlexec   := ‘alter table ‘ || v_mms_user_tab || ‘ drop partition ‘ ||

                   v_partpreflag;

    v_sqlcursor := dbms_sql.open_cursor;

    dbms_sql.parse(v_sqlcursor, v_sqlexec, dbms_sql.native);

    v_rows := dbms_sql.execute(v_sqlcursor);

    dbms_sql.close_cursor(v_sqlcursor);

    dbms_output.put_line(v_partpreflag || ‘ dropped’);

  end;

  commit;

  insert into bip_log_stat_exec_tab

  values

    (to_char(sysdate, ‘yyyymmddhh24miss’),

     ‘bip_mms_partition_proc_del’,

     ‘end’);

  commit;

exception

  when others then

    begin

      rollback;

      dbms_output.put_line(to_char(sqlcode));

      verrinfo := substr(sqlerrm, 1, 200);

      dbms_output.put_line(to_char(verrinfo));

      insert into bip_log_stat_exec_tab

      values

        (to_char(sysdate, ‘yyyymmddhh24miss’),

         ‘bip_mms_partition_proc_error’,

         verrinfo);

      commit;

    end;

end bip_mms_partition_proc;

 

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐