根據源庫表空間實際使用建立表空間指令碼

studywell發表於2017-04-14
根據源庫表空間實際使用建立表空間指令碼
經常遷移資料庫,建立表空間還需要繼續建立幾個檔案,今寫個指令碼方便以後操作。


點選(此處)摺疊或開啟

  1. declare
  2. ---根據表空間實際使用自動生成建立語句;
  3. ---如需要修改資料檔案建立路徑,修改v_path引數即可;
  4. --2017.4.14 19:18
  5. ---Edit by Well
  6.   v_path varchar2(100) := '''/data/ciq/';
  7.   i integer;
  8.   j integer;
  9.   v_tbname varchar2(50);
  10.   v_tbused varchar2(50);
  11.   cursor c_tb is
  12.     SELECT d.tablespace_name,
  13.            ceil(NVL(a.bytes - NVL(f.bytes, 0), 0) / 1024 / 1024 / 1024) as usedgb
  14.       FROM sys.dba_tablespaces d,
  15.            (select tablespace_name, sum(bytes) bytes
  16.               from dba_data_files
  17.              group by tablespace_name) a,
  18.            (select tablespace_name, sum(bytes) bytes
  19.               from dba_free_space
  20.              group by tablespace_name) f
  21.      WHERE d.tablespace_name = a.tablespace_name(+)
  22.        AND d.tablespace_name = f.tablespace_name(+)
  23.        AND NOT (d.extent_management like 'LOCAL' AND
  24.             d.contents like 'TEMPORARY')
  25.        and d.TABLESPACE_NAME not in
  26.            ('SYSAUX', 'UNDOTBS1', 'USERS', 'SYSTEM');
  27. begin
  28.   for v_tb in c_tb loop
  29.     v_tbname := v_tb.tablespace_name;
  30.     v_tbused := v_tb.usedgb;
  31.     i := ceil(v_tbused / 30);
  32.     DBMS_OUTPUT.ENABLE(buffer_size => null);
  33.     j := 1;
  34.     dbms_output.put_line('create tablespace ' || v_tbname || ' datafile ' ||
  35.                          v_path || v_tbname || '_' || j ||
  36.                          '.dbf size 100M autoextend on next 1024M'';');
  37.     for j in 2 .. i loop
  38.       dbms_output.put_line('alter tablespace ' || v_tbname ||
  39.                            ' add datafile ' || v_path || v_tbname || '_' || j ||
  40.                            '.dbf size 100M autoextend on next 1024M'';');
  41.     end loop;
  42.   end loop;
  43.   dbms_output.put_line('---for end---');
  44. end;


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

相關文章