简体   繁体   中英

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

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

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).

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