问题
Anyone have a good set of sqlplus configuration directives to help transform a given sql query into nicely tab separated output for pulling into a spreadsheet or further processing?
回答1:
Check out the Oracle documentation:
- Formatting SQLPlus Reports
- Generating HTML Reports from SQLPlus
You can generate a tab in Oracle by using the tab's ASCII value 9 and the chr function:
select chr(9) from dual;
回答2:
As Justin pointed out in his link, using the set colsep
function SQLPlus command saves typing a separator for each column.
But for tab-delimited, set colsep Chr(9)
won't work.
For UNIX or LINUX, use set colsep ' '
with the space between the single-quotes being a typed tab.
For Windows, use these settings:
col TAB# new_value TAB NOPRINT
select chr(9) TAB# from dual;
set colsep "&TAB"
select * from table;
回答3:
One particular script that I have stolen on more than one occasion comes from an AskTom thread on extracting data to a flat file. If I needed a quick and dirty flat file out of SQL*Plus. I would tend to prefer the DUMP_CSV function Tom posted earlier on that thread for any sort of ongoing process, though.
回答4:
I got a stupid solution. It worked very well.
Solution
SELECT column1 || CHR(9) || column2 || CHR(9) || column3 ... ...
FROM table
principle behind
Actually, it's just a string concatenation.
CHR(9) -> '\t'
column1 || CHR(9) || column2 -> concat(column1, '\t', column2)
回答5:
Tab characters are invisible, but, if you type the following:-
set colsep Z
but instead of the Z, press the TAB key one your keyboard, followed by enter, it works. SQLPlus understands that the next character after the space (the invisible tab) is the colsep.
I've placed all my settings into a file named /cli.sql so I just do this:-
@/cli.sql
to load them all.
set serveroutput on
SET NEWPAGE NONE
set feedback off
set echo off
set feedback off
set heading off
set colsep
set pagesize 0
SET UNDERLINE OFF
set pagesize 50000
set linesize 32767
connect use/password
(BEWARE - there is an invisible tab after an invisible space on the end of the line:)
set colsep
Enjoy!
来源:https://stackoverflow.com/questions/106323/sqlplus-settings-to-generate-tab-separated-data-file