Oracle自動新增資料檔案過程

muxinqing發表於2015-07-25
建立一個JOB定期執行以下過程就可以自動新增資料檔案
當表空間使用90%觸發過程新增資料檔案,資料檔案時按日期生成

create or replace procedure auto_add_datafile is




 ALL_file_name Varchar(500);
 file_name      Varchar(500);








 tablespace_all  varchar(500);


  Vs_Sql         Varchar2(500);






  cursor c_tablespace  is


       select a.表空間名稱,a.使用率,substr(b.FILE_NAME,0,INSTR (b.FILE_NAME, '/',1,5)) as FILE_NAME from (select t.tablespace_name "表空間名稱",round((t.full_space-nvl


 (f.free_space,0))/t.full_space*100,0) "使用率"from (select tablespace_name,round


 (sum(bytes)/(1024*1024),2) free_space from dba_free_space  group by tablespace_name) f ,
 (select tablespace_name,round(sum(bytes)/(1024*1024),2) full_space from dba_data_files group by


 tablespace_name) t where f.tablespace_name=t.tablespace_name(+)) a,dba_data_files b where a.表空間名稱=b.TABLESPACE_NAME group by a.表空間名稱,a.使用率,substr(b.FILE_NAME,0,INSTR (b.FILE_NAME, '/',1,5));


 Begin
  PRO_DATA_UPDATE_LOG('auto_add_datafile', '開始', null,null);

   for tablespace_all in c_tablespace  loop










             If tablespace_all.使用率 >=80 Then




                

                
                if tablespace_all.表空間名稱='USERS' OR tablespace_all.表空間名稱='SYSAUX' OR tablespace_all.表空間名稱='SYSTEM' OR tablespace_all.表空間名稱='UNDOTBS1' then
                  
                
                
                
                
                











             ALL_file_name := tablespace_all.file_name;
             --ALL_file_name := substr(ALL_file_name,0,INSTR (ALL_file_name, '.')-1);


             ALL_file_name := ALL_file_name||tablespace_all.表空間名稱||'_'||to_char(sysdate,'yyyymmdd')||'.dbf';






             Vs_Sql := 'alter tablespace '||tablespace_all.表空間名稱||' add datafile '''||ALL_file_name||''' size 2G';


          --dbms_output.put_line(Vs_Sql);






             Execute Immediate Vs_Sql;
         
                 else
                   ALL_file_name := tablespace_all.file_name;
             --ALL_file_name := substr(ALL_file_name,0,INSTR (ALL_file_name, '.')-1);


                   ALL_file_name := ALL_file_name||tablespace_all.表空間名稱||'_'||to_char(sysdate,'yyyymmdd')||'.dbf';






                   Vs_Sql := 'alter tablespace '||tablespace_all.表空間名稱||' add datafile '''||ALL_file_name||''' size 30G';


          --dbms_output.put_line(Vs_Sql);






                   Execute Immediate Vs_Sql;
                   

                   end if;
        End If;


   end loop;
    PRO_DATA_UPDATE_LOG('auto_add_datafile', '結束', '正常',1);
 exception
   when others then

  PRO_DATA_UPDATE_LOG('auto_add_datafile',SQLERRM, '異常',0);

 End auto_add_datafile;





來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29065182/viewspace-1750374/,如需轉載,請註明出處,否則將追究法律責任。

相關文章