2014-01-31 59 views
2

我想在Oracle 11g中使用假脫機功能。在Oracle 11g中使用假脫機

我希望將整個輸出假脫機到一個文件中,並將輸出的子集假脫機到一個單獨的文件中。

在下面的例子中,我想temp_1.txt含有從A,B,C,d的數​​據和E

temp_2.txt我想數據只爲D.

sqlplus user/[email protected] 

spool on temp_1.txt  
select * from A; 
select * from B; 
select * from C; 
spool on temp_2.txt 
select * from D; 
spool off temp_2.txt 
select * from E; 

exit; 

注: -由於這是非常古老的遺留代碼,我不能爲D編寫單獨的sqlplus會話或重新排序選擇。

回答

3

如何在sqlplus腳本中完成所有操作。如果您曾運行在不同的系統上(例如Microsoft Windows),則需要更改主機命令。但是,他們也需要在shell腳本中進行更改。

spool all_queries.txt 
select * from A; 
select * from B; 
select * from C; 
spool off 

spool only_d_query.txt 
select * from D; 
spool off 

host cat only_d_query.txt >>all_queries.txt 

spool all_queries.txt append 
select * from E; 
spool off 
2

你不能那樣做。 The SPOOL command一次只允許打開一個文件;你的第二個命令是spool temp_2.txt(沒有on)會在打開第一個文件並開始寫入第二個文件之前關閉第一個文件。而off不會採取任何進一步的參數。

Usage: SPOOL { <file> | OFF | OUT } 
where <file> is file_name[.ext] [CRE[ATE]|REP[LACE]|APP[END]] 

一個解決辦法是到後臺報表的輸出到不同的文件:

spool temp_1.txt 
select * from A; 
select * from B; 
select * from C; 
spool temp_2.txt 
select * from D; 
spool temp_3.txt 
select * from E; 
spool off 

...然後所有三個文件從操作系統合二爲一,讓您的「主人」輸出文件,同時仍保留單獨的D-only文件。例如: -

cat temp_2.txt >> temp_1.txt 
cat temp_3.txt >> temp_1.txt 
rm temp_3.txt` 

就剩下temp_1.txttemp_2.txt你想,如果我理解正確的話,你的內容。當然,如果你在Windows上使用不同的方法。

或者,您可以在PL/SQL塊中運行查詢,並使用UTL_FILE將結果寫入兩個打開文件中的一個或兩個。但是這需要更多的工作,並且將文件寫入服務器 - 所以您需要權限才能寫入DIRECTORY對象,並訪問指向的底層文件系統目錄以檢索文件。