简体   繁体   English

自动提取数据-Oracle SQL Developer

[英]Automatically extracting data - Oracle SQL Developer

I have a connection to an Oracle database via SQL Developer and I want to write a query that returns a monthly set of data and then extract that data to a delimited text file. 我通过SQL Developer连接到Oracle数据库,我想编写一个查询,该查询返回每月的数据集,然后将该数据提取到定界的文本文件中。 I know how to do that just fine, what I am wondering is if there is a way to write a script to run the query and extract the data month by month for a year. 我知道该怎么做就好,我想知道是否有一种方法可以编写脚本来运行查询并逐年提取一年的数据。 That way I would kick off the script and whenever it all finishes I would have 12 text files, one for each month. 这样,我将启动脚本,每当脚本完成时,我将拥有12个文本文件,每个月一个。

I could do it manually but it is a lot of data and I would like to have it run overnight. 我可以手动进行操作,但是其中包含大量数据,我希望它可以在一夜之间运行。 The reason for doing it this way is the application we would be using the data with would run faster if we did not try to import all of that data at once. 这样做的原因是,如果我们不尝试一次导入所有数据,将使用数据的应用程序将运行得更快。 I don't even know if it is possible but if so can someone point me in the right direction? 我什至不知道是否可能,但是如果可以,有人可以向我指出正确的方向吗?

Thanks in advance. 提前致谢。

Learn SQL*Plus, this is a really powerful tool for managing Oracle Database, if you start searching how to extract data from table to *.cvs file you will find, for example, this question right away 学习SQL * Plus,这是一个管理Oracle数据库的功能非常强大的工具,如果您开始搜索如何将表中的数据提取到* .cvs文件中,您将立即发现例如此问题

If you give me a script to create a table and fill it I will show you an example how to extract data from your table. 如果您给我一个脚本来创建表格并填写表格,我将向您展示如何从表格中提取数据的示例。

First write your parameterised script: 首先编写您的参数化脚本:

define the_year=&1
define the_mon=&2

set lines etc
select * from the_table
where trunc(the_date , 'MM' ) = to_date ( '&the_year&the_mon', 'YYYYMM' )

spool extract_&the_year&the_mon.csv

/

spool off

Then a wrapper script: 然后是包装脚本:

@the_script 2014 01
@the_script 2014 02
.
.
.
@the_script 2014 12

You can get clever(ish) and generate the wrapper: 您可以变得很聪明,并生成包装器:

sppol the_wrapper.sql
select '@the_script ' || to_char ( ADD_MONTHS ( trunc(sysdate,'YYYY' ), rn-1 ), 'YYYY MM' )
from ( select rownum rn from dual connect by level < 13 );
spool off

DOn't forget the set options to make the generated script runnable (eg set verify off, set feedback off, etc). 不要忘记设置选项以使生成的脚本可运行(例如,设置验证关闭,设置反馈关闭等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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