ITPub博客

首页 > 数据库 > Oracle > 根据源库表空间实际使用创建表空间脚本

根据源库表空间实际使用创建表空间脚本

原创 Oracle 作者:studywell 时间:2017-04-14 19:22:07 0 删除 编辑
根据源库表空间实际使用创建表空间脚本
经常迁移数据库,创建表空间还需要继续创建几个文件,今写个脚本方便以后操作。


点击(此处)折叠或打开

  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博客 ” ,链接:https://blog.itpub.net/29519108/viewspace-2137313/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-03-04

  • 博文量
    307
  • 访问量
    535408