spool的作用是什么?
spool的作用可以用一句話來描述:在sqlplus中用來保存或打印查詢結果。
參數指南
對於SPOOL數據的SQL,最好要自己定義格式,以方便程序直接導入,SQL語句如:
select taskindex||'|'||commonindex||'|'||tasktype||'|'||to_number(to_char(sysdate,'YYYYMMDD')) from ssrv_sendsms_task;
spool常用的設置
set colsep' '; //域輸出分隔符
set echo off; //顯示start啟動的腳本中的每個sql命令,缺省為on
set feedback off; //回顯本次sql命令處理的記錄條數,缺省為on
set heading off; //輸出域標題,缺省為on
set pagesize 0; //輸出每頁行數,缺省為24,為了避免分頁,可設定為0。
set termout off; //顯示腳本中的命令的執行結果,缺省為on
set trimout on; //去除標准輸出每行的拖尾空格,缺省為off
set trimspool on; //去除重定向(spool)輸出每行的拖尾空格,缺省為off
導出文本數據的建議格式:
SQL*PLUS環境設置SET NEWPAGE NONE SET HEADING OFF SET SPACE 0 SET PAGESIZE 0 SET TRIMOUT ON SET TRIMSPOOL ON SET LINESIZE 2500
注:LINESIZE要稍微設置大些,免得數據被截斷,它應和相應的TRIMSPOOL結合使用防止導出的文本有太多的尾部空格。但是如果LINESIZE設置太大,會大大降低導出的速度,另外在WINDOWS下導出最好不要用PLSQL導出,速度比較慢,直接用COMMEND下的SQLPLUS命令最小化窗口執行。
對於字段內包含很多回車換行符的應該給與過濾,形成比較規矩的文本文件。通常情況下,我們使用SPOOL方法,將數據庫中的表導出為文本文件的時候會采用兩種方法,如下述:
方法一:采用以下格式腳本
set colsep '|' --設置|為列分隔符 set trimspool on set linesize 120 set pagesize 2000 set newpage 1 set heading off set term off set num 18 set feedback off spool 路徑+文件名 select * from tablename; spool off
方法二:采用以下腳本
set trimspool on set linesize 120 set pagesize 2000 set newpage 1 set heading off set term off spool 路徑+文件名 select col1||','||col2||','||col3||','||col4||'..' from tablename; spool off
比較以上方法,即方法一采用設定分隔符然后由sqlplus自己使用設定的分隔符對字段進行分割,方法二將分隔符拼接在SELECT語句中,即手工控制輸出格式。
在實踐中,發現通過方法一導出來的數據具有很大的不確定性,這種方法導出來的數據再由sqlldr導入的時候出錯的可能性在95%以上,尤其對大批量的數據表,如100萬條記錄的表更是如此,而且導出的數據文件狂大。
而方法二導出的數據文件格式很規整,數據文件的大小可能是方法一的1/4左右。經這種方法導出來的數據文件再由sqlldr導入時,出錯的可能性很小,基本都可以導入成功。
因此,實踐中我建議大家使用方法二手工去控制spool文件的格式,這樣可以減小出錯的可能性,避免走很多彎路。
自測例:將ssrv_sendsms_task表中的數據導出到文本(數據庫Oracle 9i 操作系統 SUSE LINUX Enterprise Server 9)
spool_test.sh腳本如下:
#!/bin/sh DB_USER=zxdbm_ismp #DB USER DB_PWD=zxin_smap #DB PASSWORD DB_SERV=zx10_40_43_133 #DB SERVICE NAME sqlplus -s $DB_USER/$DB_PWD@$DB_SERV< set trimspool on set linesize 120 set pagesize 2000 set newpage 1 set heading off set term off spool promt.txt select taskindex||'|'||commonindex||'|'||tasktype||'|'||to_number(to_char(sysdate,'YYYYMMDD')) from ssrv_sendsms_task; spool off EOF
執行./spool_test.sh后生成sp_test.txt,內容如下:
83|115|1|20080307
85|115|11|20080307
86|115|10|20080307
84|115|2|20080307
6|5|14|20080307
7|5|12|20080307
9|5|15|20080307
注:上面自測例中,spool promt.txt中的目標生成文件promt.txt,在HP-UNX環境下的shell腳本中調用Oracle的spool函數,如果將上述邏輯代碼封裝為一個function,然后來調用這個function的話,則在shell腳本中最終是不會生成promt.txt文件的。只能直接執行邏輯代碼,封裝后則spool函數失效。
對於promt.txt在相對路徑下,下面2中方法在shell環境中執行時,兩者只能擇一,兩者並存則spool函數會失效。假設promt.txt文件生成的路徑為:/home/zxin10/zhuo/batchoperate/spoolfile
方式[1]
echo "start spool in shell.." sqlplus -s zxdbm_ismp/zxin_smap< set pagesize 0 set echo off feed off term off heading off trims off set colsep '|' set trimspool on set linesize 10000 set trimspool on set linesize 120 set newpage 1 spool /home/zxin10/zhuo/batchoperate/spoolfile/promt.txt select batchindex||'|'||productid||'|'||contentid||'|'||optype||'|'||uploadfile from zxdbm_700.s700_batch_operation where status=1; spool off EOF echo "end.."
方式[2]
echo "start spool in shell.." cd /home/zxin10/zhuo/batchoperate/spoolfile sqlplus -s zxdbm_ismp/zxin_smap< set pagesize 0 set echo off feed off term off heading off trims off set colsep '|' set trimspool on set linesize 10000 set trimspool on set linesize 120 set newpage 1 spool promt.txt select batchindex||'|'||productid||'|'||contentid||'|'||optype||'|'||uploadfile from zxdbm_700.s700_batch_operation where status=1; spool off EOF echo "end.."