加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Oracle作业实现存储过程动态表的创建

(2010-08-22 10:02:16)
标签:

杂谈

分类: oracle

确保权限:

select * from dba_role_privs where grantee='LOGANALYSER';

创建存储过程:

create or replace procedure p_create_table
is
v_count number(10);
tableName VARCHAR2(20);

begin
     tableName := 'LOGDETAILS_'||to_char( sysdate,'yyyymmdd' );
     --oracle存储过程实现mysql语法中的drop table  table_name if exists
     select count(*)
     into v_count
     from user_objects
     where object_name = upper( tableName);
     if v_count > 0 then
      execute immediate 'drop table ' || tableName ||' cascade constraints';
      end if;
      --动态建表 Oracle中存储过程不可以执行DDL语句,但是我们可以利用动态sql语句来建立表格
      Execute Immediate 'create table '||tableName||'
      (
       ID            NUMBER(11) not null,
       SYSTEM_ID     NUMBER(11),
       SOURCETYPE_ID NUMBER(11),
       IP            NVARCHAR2(20),
       HITTIME       TIMESTAMP(6) not null,
       URL_SOURCE    NVARCHAR2(1024),
       URL           NVARCHAR2(1000),
       SIZES         NUMBER(10,2) not null,
       STATUS        NVARCHAR2(7),
       SERVERINFO    NVARCHAR2(100),
       SERVERSOURCE  NVARCHAR2(50),
       TOOLS         NVARCHAR2(1024)
       )tablespace TBS_LOGANALYSER
           pctfree 10
           initrans 1
           maxtrans 255
           storage
           (
              initial 1
              next 1
              minextents 1
              maxextents unlimited
              pctincrease 0
           )';
      --添加约束
      Execute Immediate  'alter table '||tableName||' add constraint PK_'||tableName||' Primary key(ID)';
      commit;
   exception --异常回滚
   when others
   then
   rollback;
end p_create_table;

测试存储过程:

begin
  -- Call the procedure
  p_create_table;
end

创建作业:

declare
job number;

begin
sys.dbms_job.submit(job,'p_create_table',sysdate,'trunc(sysdate+1)+1/24'); --每天深更半夜1点闹鬼的时候调用
end;

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有