關於spool(spool是sqlplus的命令,不是sql語法裡面的東西。)
對於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<# -s 引數遮蔽列印到螢幕上的其他資訊,只顯示sql執行後從db中查詢出來的資訊,過濾掉spool函式執行時在檔案中寫入的其他資訊。
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_smapselect batchindex||'|'||productid||'|'||contentid||'|'||optype||'|'||uploadfile from zxdbm_700.s700_batch_operation where status=1;
spool off
eofecho "end.."
方式[2]
echo "start spool in shell.."
cd /home/zxin10/zhuo/batchoperate/spoolfile
sqlplus -s zxdbm_ismp/zxin_smapselect batchindex||'|'||productid||'|'||contentid||'|'||optype||'|'||uploadfile from zxdbm_700.s700_batch_operation where status=1;
spool off
eofecho "end.."
----------------------------------
如果使用者的機器上裝有oracle的客戶端,
給使用者乙個批處理檔案totxt.bat和乙個指令碼檔案totxt.sql,
totxt.bat檔案內容是:
sqlplus /nolog @totxt.sql
totxt.sql檔案的內容是:
conn username/passwd@conn_str
set head off
set pages 999
set echo off
set feed off
set lines 150
spool c:\test.txt
select ....;
spool off
exit
python將excel檔案變成txt檔案
import xlrd def row2str row data values for i in range len row data if i len row data 1 values values str row data i else values values str row data i...
如何正確把Oracle資料匯出成Txt 的格式?
以下主要介紹的是如何正確的把oracle資料匯出成txt 的格式,oracle資料匯出成txt 在實際應用中的比例還是比較常見的,以下的文章主要是對實際操作方案的描述,以下就是正文的主要內容的描述。oracle資料庫提供的sql功能很好很強大,用得越多越覺得自己淺薄無知。上週做系統演示時需要手工製造...
將目錄下所有檔案名字列出並匯出為txt檔案
目錄 windows下 ubuntu下 使用如下的dos命令來實現 dir s b lists.txt可以將當前路徑下的所有檔案的 檔案路徑 檔名 儲存在lists.txt中。其中,s表示的是 列出完整路徑 選項,如果命令列是如下形式 dir b lists.txt那麼,lists.txt檔案中只會...