oracle SQLPlus 设置以生成制表符分隔的数据文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/106323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:30:18  来源:igfitidea点击:

SQLPlus settings to generate tab-separated data file

oracletabssqlplus

提问by user19387

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?

任何人都有一套很好的 sqlplus 配置指令来帮助将给定的 sql 查询转换为很好的制表符分隔输出,以便拉入电子表格或进一步处理?

采纳答案by Eddie Awad

Check out the Oracle documentation:

查看 Oracle 文档:

You can generate a tab in Oracle by using the tab's ASCII value 9 and the chr function:

您可以通过使用选项卡的 ASCII 值 9 和 chr 函数在 Oracle 中生成选项卡:

select chr(9) from dual;

回答by user158017

As Justin pointed out in his link, using the set colsepfunction SQLPlus command saves typing a separator for each column.

正如贾斯汀在他的链接中指出的那样,使用set colsep函数 SQLPlus 命令可以节省为每列输入一个分隔符。

But for tab-delimited, set colsep Chr(9)won't work.

但是对于制表符分隔,set colsep Chr(9)不起作用。

For UNIX or LINUX, use set colsep ' 'with the space between the single-quotes being a typed tab.

对于 UNIX 或 LINUX,使用set colsep ' '单引号之间的空格作为键入的制表符。

For Windows, use these settings:

对于 Windows,请使用以下设置:

col TAB# new_value TAB NOPRINT
select chr(9) TAB# from dual;
set colsep "&TAB"

select * from table;

回答by Justin Cave

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.

我不止一次窃取的一个特定脚本来自一个 AskTom 线程,该线程将数据提取到平面文件。如果我需要从 SQL*Plus 中获取一个快速而肮脏的平面文件。不过,对于任何类型的正在进行的过程,我倾向于更喜欢 Tom 早些时候在该线程上发布的 DUMP_CSV 函数。

回答by Marvin W

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'

CHR(9) ->'\t'

column1 || CHR(9) || column2 ->concat(column1, '\t', column2)

第 1 列 || CHR(9) || column2 -> concat(column1, '\t', column2)

回答by Chris Drake

Tab characters are invisible, but, if you type the following:-

制表符是不可见的,但是,如果您键入以下内容:-

set colsep Z

设置 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.

但是不是 Z,而是按键盘上的 TAB 键,然后按 Enter,它可以工作。SQLPlus 知道空格之后的下一个字符(不可见的制表符)是 colsep。

I've placed all my settings into a file named /cli.sql so I just do this:-

我已将所有设置放入一个名为 /cli.sql 的文件中,所以我只是这样做:-

@/cli.sql

@/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!

享受!