简体   繁体   中英

Headers are not fully exporting when Oracle SQL to CSV Export with Batch File

I have created a batch file to export Oracle SQL data to CSV file, when running only the query in SQL developer it's showing as expected but when I am executing the sql script with command prompt file data is accurate but the headers are not fully showing as expected only few characters are displaying.

Below are my SQL to extract the data to CSV file,

SET VERIFY OFF  SERVEROUTPUT ON WRAP OFF
COLUMN TXT FORMAT A121 WORD_WRAPPED
SET GENERATE_HEADER = 'NO'
COL OBJECT_TYPE FORMAT A10000
COL OBJECT_NAME FORMAT A10000
SET TAB OFF
Set Newpage none
SET TRIMOUT ON
SET TRIMSPOOL ON
SET FEEDBACK OFF;
SET LINESIZE 32767
SET NUMWIDTH 120
SET COLSEP     ,
SET PAGESIZE 50000 EMBEDDED ON
spool D:\salesexport\export.csv 
SELECT * FROM NUM_EMPLOYEES;
EXIT
spool off

SQL Developer has built-in tools for generating CSV output .

One way:

select /*csv*/ * from scott.emp;

(no spaces around csv ) then use the Run As Script button. Or, run as normal, right-click in the results grid and choose 'Export'.

However, your issue seems to be in SQL*Plus.

If you have SQL*Plus 12.2 or later (note this is the Oracle client version and not the database version), you can use set markup csv :

set markup csv on

For example:

SQL*Plus: Release 19.0.0.0.0 - Production on Sat Aug 20 10:36:05 2022
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.

Last Successful login time: Sat Aug 20 2022 10:35:40 +01:00

Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0

SQL> set markup csv on
SQL>
SQL> select * from scott.emp;

"EMPNO","ENAME","JOB","MGR","HIREDATE","SAL","COMM","DEPTNO"
7369,"SMITH","CLERK",7902,"17-DEC-80",800,,20
7499,"ALLEN","SALESMAN",7698,"20-FEB-81",1600,300,30
7521,"WARD","SALESMAN",7698,"22-FEB-81",1250,500,30
7566,"JONES","MANAGER",7839,"02-APR-81",2975,,20
7654,"MARTIN","SALESMAN",7698,"28-SEP-81",1250,1400,30
7698,"BLAKE","MANAGER",7839,"01-MAY-81",2850,,30
7782,"CLARK","MANAGER",7839,"09-JUN-81",2450,,10
7788,"SCOTT","ANALYST",7566,"19-APR-87",3000,,20
7839,"KING","PRESIDENT",,"17-NOV-81",5000,,10
7844,"TURNER","SALESMAN",7698,"08-SEP-81",1500,0,30
7876,"ADAMS","CLERK",7788,"23-MAY-87",1100,,20
7900,"JAMES","CLERK",7698,"03-DEC-81",950,,30
7902,"FORD","ANALYST",7566,"03-DEC-81",3000,,20
7934,"MILLER","CLERK",7782,"23-JAN-82",1300,,10

14 rows selected.

SQL>

For earlier versions of SQL*Plus, you will have to hand-code the query including all formatting (to_number, to_date etc) and concatenation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM