How do I spool to a CSV formatted file using SQLPLUS?

后端 未结 16 1144
执笔经年
执笔经年 2020-11-22 15:40

I want to extract some queries to a CSV output format. Unfortunately, I can\'t use any fancy SQL client or any language to do it. I must use SQLPLUS.

How do I do it?

16条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 16:44

    Use vi or vim to write the sql, use colsep with a control-A (in vi and vim precede the ctrl-A with a ctrl-v). Be sure to set the linesize and pagesize to something rational and turn on trimspool and trimout.

    spool it off to a file. Then...

    sed -e 's/,/;/g' -e 's/ *{ctrl-a} */,/g'  {spooled file}  > output.csv
    

    That sed thing can be turned into a script. The " *" before and after the ctrl-A squeezes out all the useless spaces. Isn't it great that they bothered to enable html output from sqlplus but NOT native csv?????

    I do it this way because it handles commas in the data. I turns them to semi-colons.

提交回复
热议问题