0

I am trying to substitute a Python/pandas export process and go direct from Oracle to csv. I have seen a couple of posts such as this one here

Suppose I have a table in Oracle with three columns: ColA, ColB, ColC. I want to employ a command line utility that takes as input an SQL command and generates a CSV file that would look like any standard CSV file with a header line and rows of values:

ColA,ColB,ColC
A,B,C
D,E,F
...

In SQL Server, there is a utility called bcp I can use which looks like this:

bcp "SELECT 'ColA', 'ColB', 'ColC' UNION ALL Select ColA, ColB, ColC From 
myTable" queryout "C:\Temp\csvfiles\output.csv" -c -CRAW -S localhost -u 
my_userid -p my_password -d my_database

Easy-peasy. But in looking for the Oracle equivalent, not so much.

I took a look here but the author wants you to create a UTL_FILE. That doesn't seem like a possibility since this is supposed to be a completely generic process that gets built and run from Python. I took a look at sqlplus but I can't seem to come up with a way to create a typical CSV similar to bcp. The docs here weren't terribly clear on this either. It's such a common format I expected to see tons of examples.

Sites like this here show how to output a fixed-length CSV using sqlplus but don't show you how to add a header row and values are padded, which might be OK, but not desirable. This was about the best I could come up with:

set colsep ,
set headsep off
set pagesize 0
set trimspool on
set linesize 200
set numwidth 50
SET TERMOUT OFF

Spool output.csv

SELECT ColA,
    ColB,
    ColC
FROM MyTable;

Spool off
exit

So, is there an equivalent tool/command in Oracle to the trusty bcp, or is there a sample somewhere using sqlplus that can generate a csv similar to the one above with a header line and without all the leading spaces? Or is there another option to generate dynamic CSV files from random Oracle tables? Or am I stuck using Python, pyodbc and pandas to get my nicely-formatted CSV files? TIA

0

2 Answers 2

2

Oracle's SQLcl utility(commandline executable sql or sql.exe) could help you achieve it.

Here's the download link SQLcl. It's free.

In order to export a file in the CSV format, you may simply specify

SET SQLFORMAT CSV

If you want a different delimiter, use

SET SQLFORMAT DELIMITED '|'

Here's a sample output with the header

SQL> select * from employees;
"EMPLOYEE_ID","FIRST_NAME","LAST_NAME","EMAIL","PHONE_NUMBER","HIRE_DATE","JOB_ID","SALARY","COMMISSION_PCT","MANAGER_ID","DEPARTMENT_ID"
    198,"Donald","OConnell","DOCONNEL","650.507.9833",21-06-07,"SH_CLERK",2600,,124,50
    199,"Douglas","Grant","DGRANT","650.507.9844",13-01-08,"SH_CLERK",2600,,124,50
    200,"Jennifer","Whalen","JWHALEN","515.123.4444",17-09-03,"AD_ASST",4400,,101,10
    201,"Michael","Hartstein","MHARTSTE","515.123.5555",17-02-04,"MK_MAN",13000,,100,20
    202,"Pat","Fay","PFAY","603.123.6666",17-08-05,"MK_REP",6000,,201,20
    203,"Susan","Mavris","SMAVRIS","515.123.7777",07-06-02,"HR_REP",6500,,101,40
    204,"Hermann","Baer","HBAER","515.123.8888",07-06-02,"PR_REP",10000,,101,70
    205,"Shelley","Higgins","SHIGGINS","515.123.8080",07-06-02,"AC_MGR",12008,,101,110
    206,"William","Gietz","WGIETZ","515.123.8181",07-06-02,"AC_ACCOUNT",8300,,205,110
    100,"Steven","King","SKING","515.123.4567",17-06-03,"AD_PRES",24000,,,90
    101,"Neena","Kochhar","NKOCHHAR","515.123.4568",21-09-05,"AD_VP",17000,,100,90
    102,"Lex","De Haan","LDEHAAN","515.123.4569",13-01-01,"AD_VP",17000,,100,90
    103,"Alexander","Hunold","AHUNOLD","590.423.4567",03-01-06,"IT_PROG",9000,,102,60
    104,"Bruce","Ernst","BERNST","590.423.4568",21-05-07,"IT_PROG",6000,,103,60
    105,"David","Austin","DAUSTIN","590.423.4569",25-06-05,"IT_PROG",4800,,103,60
    106,"Valli","Pataballa","VPATABAL","590.423.4560",05-02-06,"IT_PROG",4800,,103,60
    107,"Diana","Lorentz","DLORENTZ","590.423.5567",07-02-07,"IT_PROG",4200,,103,60
    108,"Nancy","Greenberg","NGREENBE","515.124.4569",17-08-02,"FI_MGR",12008,,101,100
    109,"Daniel","Faviet","DFAVIET","515.124.4169",16-08-02,"FI_ACCOUNT",9000,,108,100

Additionally, all the SQL* Plus commands are supported with few more specific to SQLcl. See this command line reference

Sign up to request clarification or add additional context in comments.

Comments

0

SET MARKUP CSV ON - use this in sqlplus before executing the query to get the data in csv format

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.