简体   繁体   中英

How to spool in Oracle properly

I've created a spool command to export data from the Oracle into the csv. file. Here it is:

    set echo off
    set verify off
    set feedback off
    set heading on
    set termout on
    set pages 50000
    set newpage none
    spool Y:\Marketing\Nick\OptivoList\test.csv
    Select EMAIL || ',' || ',' || GENDER
    from Email_test;
    spool off;
    exit;

It creates output like that:

EMAIL,GENDER
-----------------
test1@test.com, M
test2@test.com, W
test3@test.com, M

The question- how to get rid that '----------' output between the headers and the data?

There are two ways -

  1. set underline off

For example,

SQL> set underline off
SQL> select empno, ename from emp;

     EMPNO,ENAME
      7369,SMITH
      7499,ALLEN
      7521,WARD
      7566,JONES
      7654,MARTIN
      7698,BLAKE
      7782,CLARK
      7788,SCOTT
      7839,KING
      7844,TURNER
      7876,ADAMS
      7900,JAMES
      7902,FORD
      7934,MILLER

14 rows selected.

SQL>
  1. Custom Column headers with pagesize 0

You need to set pagesize to zero. This will suppress the column headers and repeating column headers. And then, apply your custom column name using an UNION ALL statement.

For example,

SQL> set pagesize 0
SQL> SELECT 'EMPNO' ||' , '|| 'ENAME' FROM DUAL
  2   UNION ALL
  3   SELECT EMPNO||' , '||ENAME FROM EMP
  4   /
EMPNO , ENAME
7369 , SMITH
7499 , ALLEN
7521 , WARD
7566 , JONES
7654 , MARTIN
7698 , BLAKE
7782 , CLARK
7788 , SCOTT
7839 , KING
7844 , TURNER
7876 , ADAMS
7900 , JAMES
7902 , FORD
7934 , MILLER

15 rows selected.

SQL>

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