<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>spool &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/spool/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Fri, 21 Apr 2023 06:07:28 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>spool &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>使用 Oracle sqlplus 將查詢結果匯出到檔案</title>
		<link>https://noter.tw/9109/oracle-sqlplus-export-results-to-file/</link>
					<comments>https://noter.tw/9109/oracle-sqlplus-export-results-to-file/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 11 Aug 2021 11:54:28 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[技術相關]]></category>
		<category><![CDATA[資料庫]]></category>
		<category><![CDATA[linesize]]></category>
		<category><![CDATA[另存檔案]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[spool]]></category>
		<category><![CDATA[sqlplus]]></category>
		<category><![CDATA[匯出搜尋結果]]></category>
		<category><![CDATA[匯出檔案]]></category>
		<category><![CDATA[查詢結果]]></category>
		<category><![CDATA[colsep]]></category>
		<guid isPermaLink="false">https://noter.tw/?p=9109</guid>

					<description><![CDATA[<p>小蛙又遇到不能用 GUI 來管理 Oracle 的專案了，其實跟 2012 年的時候遇到的這個是相同的專案（Oracle 使用 sqlplus 匯入 SQL 檔案），該篇記錄了怎麼把一堆要執行的 SQ&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/9109/oracle-sqlplus-export-results-to-file/" data-wpel-link="internal">使用 Oracle sqlplus 將查詢結果匯出到檔案</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>小蛙又遇到不能用 GUI 來管理 Oracle 的專案了，其實跟 2012 年的時候遇到的這個是相同的專案（<a href="https://noter.tw/2371/oracle-sqlplus-import-sql-file/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">Oracle 使用 sqlplus 匯入 SQL 檔案</a>），該篇記錄了怎麼把一堆要執行的 SQL 指令存入檔案中，並於 Oracle sqlplus 載入檔案執行，這篇文章為反向動作，將查詢的結果輸出到檔案內。</p>



<span id="more-9109"></span>



<p>這個專案的環境很麻煩，在一個需要透過 VPN 連進去的內部環境，連上 VPN 之後就 &#8230; 斷網了，完全沒辦法對外，沒辦法 Google 查資料，對小蛙這種不專精的半調子來說存在一定難度，幸好有強大的同仁用 VM 的特殊大絕解決這問題，不過 VM 裡面連上 VPN 還是不能上網，但總算可以在本機 Google 資料，再手動把指令或語法手動輸入進去，對，沒錯，沒辦法複製貼上 Orz &#8230;</p>



<p>囉嗦一堆，進正題</p>



<pre class="wp-block-preformatted withcode">set colsep ,
set headsep off
set pagesize 0
set linesize 10000
set trimspool on
set echo off

spool /home/hans/output

select * from image;

spool off</pre>



<p class="withcode">上面那些指令是參考 <a href="https://stackoverflow.com/" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">StackOverflow<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 多個答案來的，有些實際上在做什麼小蛙也不是很了解，但還是可以猜出大概是什麼，例如：<code>colset</code> 是設定欄位間的分隔符號(其他詳細設定請自行 Google)。小蛙遇到有問題的地方是 <code>linesize</code> 沒有設定，導致匯出的每一行都被切斷在莫名其妙的地方。其他 <code>spool 輸出檔案路徑</code>、<code>select * from image;</code> 要匯出結果的查詢 &#8230; 等等。</p>



<p>這指令已符合小蛙的需求，先備存，之後有其他需求再去查詳細的資料。以下是小蛙自己留存用的</p>



<pre class="wp-block-preformatted withcode">// 相當於 mysql show tables;
select table_name from user_tables;

// 查找排序過的前 10 筆 (Top 10)
select rownum, t.* from ( 實際要運作的 SQL ) t where rownum &lt; 10;

// 列出表格的所有欄位
select * from user_tab_columns A where A.table_name = 'IMAGE';
// 列出名稱、類型及長度
select column_name, data_type, default_length from user_tab_columns A where A.table_name = 'IMAGE';

// 複製 table 內所有資料
create table image_backup as select * from image;

// 表格建立的時間
select object_name, created, TIMESTAMP from user_objects where object_name = 'IMAGE';

// 修改表格
alter table IMAGE modify (file_name varchar2(300), index_term varchar2(1000));

// 格式變動完成
commit;</pre>



<p><strong>Oracle 系列文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/11178/%e4%bf%ae%e6%94%b9-oracle-sequence/" data-wpel-link="internal">修改 Oracle Sequence</a><a href="https://noter.tw/2371/oracle-%e5%8c%af%e5%85%a5-sql-%e6%aa%94%e6%a1%88/" data-wpel-link="internal"></a></li><li><a href="https://noter.tw/2371/oracle-sqlplus-import-sql-file/" data-wpel-link="internal">Oracle 使用 sqlplus 匯入 SQL 檔案</a></li><li><a href="https://noter.tw/9109/oracle-sqlplus-export-results-to-file/" data-wpel-link="internal">使用 Oracle sqlplus 將查詢結果匯出到檔案</a></li><li><a href="https://noter.tw/2714/%e5%ae%98%e6%96%b9-client-oracle-sql-developer-%e5%ae%89%e8%a3%9d%e6%95%99%e5%ad%b8/" data-wpel-link="internal">官方 Client – Oracle SQL Developer 安裝教學</a></li><li><a href="https://noter.tw/2711/oracle-%e5%8c%af%e5%85%a5%e5%8c%af%e5%87%ba-excel-%e9%80%8f%e9%81%8e-sql-developer/" data-wpel-link="internal">Oracle 匯入/匯出 Excel (透過 SQL Developer)</a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/9109/oracle-sqlplus-export-results-to-file/" data-wpel-link="internal">使用 Oracle sqlplus 將查詢結果匯出到檔案</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/9109/oracle-sqlplus-export-results-to-file/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Oracle 使用 sqlplus 匯入 SQL 檔案</title>
		<link>https://noter.tw/2371/oracle-sqlplus-import-sql-file/</link>
					<comments>https://noter.tw/2371/oracle-sqlplus-import-sql-file/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Tue, 16 Oct 2012 12:30:50 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[start file]]></category>
		<category><![CDATA[@]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[spool]]></category>
		<category><![CDATA[sql]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2371</guid>

					<description><![CDATA[<p>小蛙上星期遇到一個棘手的問題，也才知道原來 Oracle 沒有了 GUI 介面之後是那麼樣的難操作 &#8230; 這篇文章紀錄一下關於如何把資料透過 Oracle start file (@ fil&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2371/oracle-sqlplus-import-sql-file/" data-wpel-link="internal">Oracle 使用 sqlplus 匯入 SQL 檔案</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>小蛙上星期遇到一個棘手的問題，也才知道原來 Oracle 沒有了 GUI 介面之後是那麼樣的難操作 &#8230; 這篇文章紀錄一下關於如何把資料透過 <strong>Oracle start file (@ file)</strong> 的方式匯入沒有 GUI 介面的 Oracle。 </p>



<span id="more-2371"></span>



<p>聽起來好像不是什麼很大的問題，對大部分的資料庫老手來說，但對小蛙真的是個大問題，一直以來都是透過 GUI 操作 Oracle，真正遇到文字介面的時候還慌了一大下，問題是這樣的，某個 Tomcat + Oracle 的環境下，Tomcat 跟 Oracle 各為兩台不同機器，Tomcat 有對外 IP，而 Oracle 只能透過 Tomcat SSH (虛擬IP) 進去操作，由於環境內部幾乎所有 port 都被擋掉，連請同事打 tunnel 都沒辦法的情況下，只好使出將要匯入的資料先整理成 sql 檔案再匯入。 </p>



<p>原本使用 PL/SQL 的 Export data 匯出 SQL Inserts，卻出現 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">Table xxx contains one or more CLOB columns.
Cannot export in SQL format, user PL/SQL Developer format instead.</pre>



<p>如果使用 Oracle Export 會沒辦法達成小蛙要的需求(兩邊資料庫是不同的，加上還要額外另外匯入 Excel 的資料)，只好乖乖寫程式把要匯入的資料整理成 SQL Inserts format。怎麼把資料或 Excel 匯出成 SQL format 就不贅述了，有幾點要注意： </p>



<ul class="my-li bg-darkblue wp-block-list"><li>原本程式裡的 <strong>PreparedStatement</strong> 要全部寫成 <strong>Statement</strong>。</li><li>從 Excel 撈出的資料要檢查其正確性(單雙引號等特殊字元)。</li><li>JXL 對於 Date Format 的 Cell 撈出來後會有問題(這個在另一篇詳述)。 </li></ul>



<p>資料全部匯出之後，在 Oracle 機器下進入<strong> sqlplus</strong>，首先小蛙第一次忘了把結果紀錄下來(spool filename)，到最後根本不知道哪些有問題哪些沒問題，只好乖乖的把資料刪掉重匯(好死不死竟然輸入了commit;)，這該死的資料庫，一個主表帶著一堆子表，並擁有 FK 關係，更令人噴飯的是通通沒有設定 <strong>on delete cascade</strong> &#8230; 只好，先一個一個子表清光光後，再清主表(這邊其實可以把既有的 FK 刪除，再新增有 on delete cascade 屬性的 FK，而且很怪的是 PL/SQL 可以使用 <strong>delete from table cascade</strong> 的語法，而在 Oracle sqlplus 中小蛙輸入卻發生錯誤)，好，清完之後第二次謹慎點！ </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">SQL&gt; spool import_result.log
SQL&gt; @ import_file_name.sql
新增一筆資料
新增一筆資料
...
...
新增一筆資料
SQL&gt; commit;
SQL&gt; spool off</pre>



<p>終於，大功告成。如果有更好的做法，還請各位高手不吝留言給小蛙，感恩萬分！ </p>



<h2 class="para wp-block-heading">參考資料</h2>



<ol class="my-li bg-darkblue wp-block-list"><li><a href="http://hsinichi.pixnet.net/blog/post/5321280" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">Oracle Database &#8211; SQLPLUS常用指令 @&nbsp;資訊理想化的延伸<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li></ol>



<p><strong>Oracle 系列文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/11178/%e4%bf%ae%e6%94%b9-oracle-sequence/" data-wpel-link="internal">修改 Oracle Sequence</a><a href="https://noter.tw/2371/oracle-%e5%8c%af%e5%85%a5-sql-%e6%aa%94%e6%a1%88/" data-wpel-link="internal"></a></li><li><a href="https://noter.tw/2371/oracle-sqlplus-import-sql-file/" data-wpel-link="internal">Oracle 使用 sqlplus 匯入 SQL 檔案</a></li><li><a href="https://noter.tw/9109/oracle-sqlplus-export-results-to-file/" data-wpel-link="internal">使用 Oracle sqlplus 將查詢結果匯出到檔案</a></li><li><a href="https://noter.tw/2714/%e5%ae%98%e6%96%b9-client-oracle-sql-developer-%e5%ae%89%e8%a3%9d%e6%95%99%e5%ad%b8/" data-wpel-link="internal">官方 Client – Oracle SQL Developer 安裝教學</a></li><li><a href="https://noter.tw/2711/oracle-%e5%8c%af%e5%85%a5%e5%8c%af%e5%87%ba-excel-%e9%80%8f%e9%81%8e-sql-developer/" data-wpel-link="internal">Oracle 匯入/匯出 Excel (透過 SQL Developer)</a></li></ul>



<p><strong>Excel 相關文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/9117/excel-filter-and-validation/" data-wpel-link="internal">加入篩選器及下拉選單 (資料驗證)</a></li><li><a href="https://noter.tw/9063/excel-auto-search-and-fill-data-by-vlookup-index-match/" data-wpel-link="internal">搜尋表格 填入資料 強大的 VLOOKUP, INDEX, MATCH</a></li><li><a href="https://noter.tw/9061/split-a-excel-to-files/" data-wpel-link="internal">分拆 Excel 成多個檔案 ( Split A Excel to Files)</a></li><li><a href="https://noter.tw/6550/excel-%e4%bd%bf%e7%94%a8-poi-%e8%ae%80%e5%8f%96%e6%96%87%e5%ad%97%e6%a0%bc%e5%bc%8f%e6%97%a5%e6%9c%9f%e5%8d%bb%e8%ae%8a%e6%88%90%e6%95%b8%e5%ad%97%e7%9a%84%e5%95%8f%e9%a1%8c/" data-wpel-link="internal">Excel 使用 POI 讀取文字格式日期卻變成數字的問題</a></li><li><a href="https://noter.tw/4300/java-%e8%ae%80%e5%8f%96-excel-%e6%96%87%e4%bb%b6xls-xlsx-%e4%bd%bf%e7%94%a8-apache-poi/" data-wpel-link="internal">Java 讀取 Excel 文件(xls, xlsx) – 使用 Apache POI</a></li><li><a href="https://noter.tw/2711/oracle-%e5%8c%af%e5%85%a5%e5%8c%af%e5%87%ba-excel-%e9%80%8f%e9%81%8e-sql-developer/" data-wpel-link="internal">Oracle 匯入/匯出 Excel (透過 SQL Developer)</a></li><li><a href="https://noter.tw/1732/jsp%e5%b0%87%e8%b3%87%e6%96%99%e5%8c%af%e5%87%ba%e6%88%90excel%e7%9b%b4%e6%8e%a5%e4%b8%8b%e8%bc%89-by-jexcelapi-jxl/" data-wpel-link="internal">JSP將資料匯出成Excel直接下載 by JExcelApi (jxl)</a></li><li><a href="https://noter.tw/1569/java-excel-jxl/" data-wpel-link="internal">Java + Excel = JXL</a></li><li><a href="https://noter.tw/119/ncr-%e8%99%95%e7%90%86%e6%96%b9%e5%bc%8f%ef%bc%9ajava-vba/" data-wpel-link="internal">NCR &amp;#xxxxx; 處理方式：Java &amp; VBA</a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2371/oracle-sqlplus-import-sql-file/" data-wpel-link="internal">Oracle 使用 sqlplus 匯入 SQL 檔案</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2371/oracle-sqlplus-import-sql-file/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
