7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Oracle]sqlplusの出力をCSV形式で出すための簡単な方法

Last updated at Posted at 2019-05-25

これは

OracleからデータをCSV形式で出力する際に、クエリ芸を使うのは辞めよう、という話です。

以下をクエリ芸と(勝手に)呼びます。
パイプとクオートがゲシュタルト崩壊しそうですね><

SELECT
  '"' || SHOUHIN_ID || '"' ||
  ',"' || TO_CHAR(SHIPPING_DATE, 'YYYY/MM/DD') || '"' || 
  ',' || QUANTITY || 
  ',"' || TO_CHAR(TO_DATE('ARRIVAL_DATE', 'YYYY/MM/DD HH24:MI:SS'), 'YYYY/MM/DD HH24:MI:SS') || '"'
FROM SHOUHIN_MASTER;

ではどうするか

OracleClientの12.2以上をお使いであれば、簡単です!
(12.1以前ならアップしましょう、基本的に下位互換ですよ)

SET MARKUP CSV ON を宣言しよう

SET MARKUP CSV ON
SELECT
  SHOUHIN_ID,TO_CHAR(SHIPPING_DATE, 'YYYY/MM/DD'),QUANTITY,TO_CHAR(TO_DATE('ARRIVAL_DATE', 'YYYY/MM/DD HH24:MI:SS'), 'YYYY/MM/DD HH24:MI:SS')
FROM SHOUHIN_MASTER;

sqlplusのオプションを使おう

sqlplusのオプションに -M "csv on" を付与するだけです。

sqlplus64 -S -M 'csv on' ${ORACLE_USR}/${ORACLE_PSW}@${ORACLE_DB} @exec.sql

参考情報

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?