With Oracle 12.2.0.1, SQL * Plus is capable of displaying the result of a query as CSV.
Its use is as follows.
1 | SQL> SET MARK[UP] CSV {ON|OFF} DELIMI[TER] character QUOTE {ON|OFF} |
Options
DELIMI[TER] : The character used to separate column values. Only single characters are allowed. The default value is “,” (comma).
QUOTE : If this is ON, the column values that are text are shown in “” (double quotation marks). It can take ON or OFF values. The default value is OFF.
Generate CSV output in double quotes
1 2 3 4 5 6 7 | SQL> SET MARKUP CSV ON QUOTE ON SQL> SELECT * FROM PERSON; "ID","NAME","SURNAME" 1,"AHMET","DURUOZ" 2,"ALI","VELI" 3,"VELI","ALI" |
Generate CSV output without double quotes
1 2 3 4 5 6 7 | SQL> SET MARKUP CSV ON QUOTE OFF SQL> SELECT * FROM PERSON; ID,NAME,SURNAME 1,AHMET,DURUOZ 2,ALI,VELI 3,VELI,ALI |
Generate CSV output using “| (pipe)” as delimiter
1 2 3 4 5 6 7 | SQL> SET MARKUP CSV ON DELIMITER | SQL> SELECT * FROM PERSON; ID|NAME|SURNAME 1|AHMET|DURUOZ 2|ALI|VELI 3|VELI|ALI |
1 2 3 4 5 | [oracle@orcldb ~]$ sqlplus -S -M "csv on" / as sysdba @/home/oracle/person.sql | sed '/^$/d' "ID","NAME","SURNAME" 1,"AHMET","DURUOZ" 2,"ALI","VELI" 3,"VELI","ALI" |
1 2 3 4 5 6 7 8 | [oracle@orcldb ~]$ sqlplus -S -M "csv on" / as sysdba @/home/oracle/person.sql "ID","NAME","SURNAME" 1,"AHMET","DURUOZ" 2,"ALI","VELI" 3,"VELI","ALI" [oracle@orcldb ~]$ |