Automatically extracting data - Oracle SQL Developer

帅比萌擦擦* 提交于 2019-12-25 03:55:09

问题


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.


回答1:


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 (e.g. set verify off, set feedback off, etc).




回答2:


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.



来源:https://stackoverflow.com/questions/25184216/automatically-extracting-data-oracle-sql-developer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!