SELECT COUNT(*) INTO v_count ---獲得遊標v_curtp指向的當前資料表空間包含的資料檔案數
FROM DBA_DATA_FILES
WHERE tablespace_name=v_tp.tablespace_name;
ELSIF v_tp.CONTENTS='PERMANENT' THEN ---普通資料表空間
v_txt:='CREATE TABLESPACE '||v_tp.tablespace_name||' DATAFILE ';
insert into bak_dba_tablesapce(ddl_txt) values(v_txt);
END IF;
if v_tp.CONTENTS='TEMPORARY' THEN ----臨時資料檔案
OPEN V_CURDF for select * from dba_temp_files where tablespace_name=v_tpname;
else
OPEN V_CURDF for select * from dba_data_files where tablespace_name=v_tpname;
end if;
LOOP
FETCH v_curdf INTO v_df; ---擷取DATAFILE定義
EXIT WHEN v_CURdf%NOTFOUND;
IF V_DF.AUTOEXTENSIBLE='YES' THEN
V_DDL:='ON';
ELSE
V_DDL:='OFF';
END IF;
IF v_curdf%rowcount=v_count THEN
v_txt:=''''||v_df.file_name||''''||' SIZE '||(V_DF.BLOCKS*8/1024)||'M AUTOEXTEND '||V_DDL;
insert into bak_dba_tablesapce(ddl_txt) values(v_txt);
ELSE
v_txt:=''''||v_df.file_name||''''||' SIZE '||(V_DF.BLOCKS*8/1024)||'M AUTOEXTEND '||V_DDL||',';
insert into bak_dba_tablesapce(ddl_txt) values(v_txt);
END IF;
END LOOP;
CLOSE V_CURDF;
IF v_tp.CONTENTS='UNDO' THEN ---回退資料表空間儲存參數
insert into bak_dba_tablesapce(ddl_txt) values(V_TP.STATUS);
ELSE ---普通資料表空間、暫存資料表空間儲存參數
IF v_tp.CONTENTS='PERMANENT' THEN ---普通資料表空間儲存參數
insert into bak_dba_tablesapce(ddl_txt) values(V_TP.LOGGING);
insert into bak_dba_tablesapce(ddl_txt) values(V_TP.STATUS);
insert into bak_dba_tablesapce(ddl_txt) values('PERMANENT');
END IF;
IF v_tp.ALLOCATION_TYPE='UNIFORM' THEN ----統一分區尺寸
v_txt:='EXTENT MANAGEMENT '||V_TP.EXTENT_MANAGEMENT||' UNIFORM SIZE '||v_tp.INITIAL_EXTENT/(1024*1024)||'M';
insert into bak_dba_tablesapce(ddl_txt) values(v_txt);
ELSIF v_tp.ALLOCATION_TYPE='SYSTEM' THEN ----系統自動管理分區尺寸
v_txt:='EXTENT MANAGEMENT '||V_TP.EXTENT_MANAGEMENT||' AUTOALLOCATE ' ;
insert into bak_dba_tablesapce(ddl_txt) values(v_txt);
END IF;
IF v_tp.SEGMENT_SPACE_MANAGEMENT='AUTO' THEN ----系統自動管理段空間
insert into bak_dba_tablesapce(ddl_txt) values('SEGMENT SPACE MANAGEMENT AUTO');
END IF;
END IF;
v_txt:='BLOCKSIZE '||(V_TP.BLOCK_SIZE/1024)||'K ';
insert into bak_dba_tablesapce(ddl_txt) values(v_txt);
insert into bak_dba_tablesapce(ddl_txt) values('/');
insert into bak_dba_tablesapce(ddl_txt) values('');
commit;
END LOOP;
CLOSE V_CURTP;
EXCEPTION
WHEN OTHERS THEN
if v_curtp%isopen then
close v_curtp;
if v_curdf%isopen then
close v_curdf;
end if;
end if;
RAISE;
END get_tabspace_ddl;
---------------------------------------------------------------------
get_tabspace_dll.sh
用於crontab 定時備份資料庫資料表空間的ddl
---------------------------------------------------------------------
#!/bin/ksh
#產生 bill資料庫的資料表空間ddl語句
#每天執行
#擷取環境變數
. /oracle/.profile
username=sys
password=aaa123
########
sqlplus username/password<<EOF
---declare var here
begin
get_tabspace_ddl;
end;
/
exit
/
EOF
if [ $? -ne 0 ];then
echo "ERROR! execute procedure failed! please check it"
#mail ...
exit 1
fi
sqlplus username/password <<!
set pages 0;
set serveroutput on size 1000000;
set heading off;
set feedback off;
set echo off;
spool /ora_backup/orasysbak/bill_tabspace_ddl.sql
select ddl_txt from bak_dba_tablesapce;
spool off;
exit
!
if [ $? -ne 0 ];then
echo "ERROR! generate tabspace ddl failed! please check it"
#mail ...
exit 1
fi