问题
I am using shell script to execute an export in sqlplus
Spool:
SET echo off
SET linesize 32767
SET LONG 1000
SET LONGCHUNKSIZE 1000
SET wrap off
SET heading off
SET colsep ';'
SET pagesize 0
SET feed off
SET termout off
SET trimspool off
SELECT
AUTO_SEQ.nextval as ROW_ID,
..
..
And the result:
29419987;FOR_IMPORT;1000 ;KR 19 F 65 73 ;KR 19 F 65 73, 47001, 47000 ;SANTA MARTA ;Cargado por EIM en 03-MAY-17 ;KR 19 F 65 73, 47001, 47000 ;KR 19 F 65 73, 47001, 47000 ;KR 19 F 65 73, 47001, 47000 ;MAGDALENA ; ;COLOMBIA; ; ; ; ; ;Y;N;Y
The result has many blanks before and/or after the data, so my question is how can I remove them ?
Thanks in advance.
回答1:
Just grab yourself the current version of SQL Plus, and you'll be good to go
SQL> set markup csv on
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,"09-DEC-82",3000,,20
7839,"KING","PRESIDENT",,"17-NOV-81",5000,,10
7844,"TURNER","SALESMAN",7698,"08-SEP-81",1500,,30
7876,"ADAMS","CLERK",7788,"12-JAN-83",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
回答2:
Setting colsep
doesn't stop the results being output in columns, and that means the values in each row are still padded out to the same length. I'd forget about colsep
and instead concatenate the values together with the delimiter:
SELECT
AUTO_SEQ.nextval
||';'|| column1
||';'|| column2
...
||';'|| columnN
FROM
...
That gives a single column expression, with no extra spaces added.
You may need to enclose string value sin double quotes if they could contain the delimiter themselves (though that depends a bit on who will consume this), and you can concatenate those in as well. You should also explicitly format dates as strings etc. if you aren't already.
来源:https://stackoverflow.com/questions/43764208/sqlplus-export-and-spool-result