Oracle中spool命令實現的兩種方法比較

2021-06-05 16:22:07 字數 2205 閱讀 4110

要輸出符合要求格式的資料檔案只需在select時用字元連線來規範格式。比如有如下表

sql>; select id,username,password from myuser;//測試表

1 john       1234

2 jack       12345

3 rose       2345

4 joe        384657

5 tom        384655

6 jordan     384455

要輸出符合1,john,1234,這樣的資料格式就用select id||','||username||','||password||',' from myuser這樣的語句。

sql>; select id||','||username||','||password||',' from myuser;

1,john,1234,

2,jack,12345,

寫個下面這樣的指令碼就行可以輸出符合要求格式的資料至檔案中,不會含有其它不需要東西,只有資料部分。

--指令碼檔名為expmyusr.sql,存資料的檔名為e:\exp.txt

set echo on            --是否顯示執行的命令內容

set feedback off       --是否顯示 *   rows   selected

set heading off        --是否顯示欄位的名稱

set verify off         --是否顯示替代變數被替代前後的語句。fil

set trimspool off      --去字段空格

set pagesize 1000      --頁面大小

set linesize 50//linesize設定盡量根據需要來設定,大了生成的檔案也大

define fil= 'e:\exp.txt'

prompt *** spooling to &fil

spool &fil

select id||','||username||','||'"'||password||'"' from myuser;

spool off;

--執行過程

sql>; @e:\expmyusr.sql

*** spooling to e:\exp.txt

1,john,"1234"

2,jack,"12345"

3,rose,"2345"

4,joe,"384657"

5,tom,"384655"

6,jordan,"384455"

檢查可知結果符合要求。

oracle spool的兩種方法之對比

方法一:採用以下格式指令碼 

set colsep '' ------設定列分隔符

set trimspool on

set linesize 120

set pagesize 2000

set newpage 1

set heading off

set term 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語句中,即手工控制輸出格式。

在實踐中,我發現通過方法一匯出來的資料具有很大的不確定性,這種方法匯出來的資料再由sql ldr匯入的時候出錯的可能性在95%以上,尤其對大批量的資料表,如100萬條記錄的表更是如此,而且匯出的資料檔案狂大。

而方法二匯出的資料檔案格式很規整,資料檔案的大小可能是方法一的1/4左右。經這種方法匯出來的資料檔案再由sqlldr匯入時,出錯的可能性很小,基本都可以匯入成功。

因此,實踐中我建議大家使用方法二手工去控制spool檔案的格式,這樣可以減小出錯的可能性,避免走很多彎路。

Oracle中SPOOL命令使用方法詳解

舉例 test.sh 如下 複製 bin sh sqlplus s scott tiger spool常用的設定 set head off 輸出域標題,預設為on set linesize 20000 linesize可以設定的大點,防止一行長度不夠 set echo off 顯示sqlplus中的...

sqlplus 中的spool命令

sqlplus nolog eof set linesize 2500 set pagesize 0 set heading off set feedback off set echo off set termout off set trimout on set trimspool on set s...

oracle中spool的用法小結

在生產中常會遇到需要將數量比較大的錶值匯入到本地文字檔案中.方法有很多種,比較常用的就是spool命令 要輸出符合要求格式的資料檔案只需在select時用字元連線來規範格式。比如有如下表 sql select id,username,password from myuser 測試表 1 john 1...