Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

oracle sql Create multiple spool files one  for each month

Posted on 2011-09-14
10
Medium Priority
?
603 Views
Last Modified: 2012-05-12

We have a query like this (ofcourse the real query is way more complicated than that)

SET NEWPAGE 0
SET SPACE 0
SET PAGESIZE 0
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
SET MARKUP HTML OFF
SET COLSEP "|"
SET TRIMSPOOL ON
SET LINES 300
spool cash_amount_jan_2009.txt

Select col1,col2
From table
Where month=’01’
And year=’2009’

Spool off


What is the best way to handle a request to produce spool files for all months since 2009 till now.
Like parametrizing the filter;or using pl/sql.

Please adise
0
Comment
Question by:it-rex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36538204
Issue multiple spool commands and repeat the queries for each month in the script.


spool Jan.txt
select --- for Jan

spool Feb.txt
select --- for Feb

...
0
 
LVL 11

Author Comment

by:it-rex
ID: 36538221

:) I have alreday done that
they are asking for some kind of automation;
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36538253
Any way you do it will be ugly.

You can use PL/SQL and UTL_FILE or possible write some OS script.

You can also have SQL generate the SQL, spool it out, and execute the new script.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 48

Expert Comment

by:schwertner
ID: 36543309
You can also try the FOR statement of OS shels and to pass parameters to SQL*Plus script that are monts and years.
The problem of my suggestion is that shell scripting languages except sophisticated like PERL are not so flexible as procedural language.
So the PL/SQL approach proposed above is the most flexible and native for Oracle.
0
 
LVL 11

Author Comment

by:it-rex
ID: 36543314
any other ideas,anyone?
0
 
LVL 9

Expert Comment

by:parparov
ID: 36543704
so, perl is not an option?
0
 
LVL 11

Author Comment

by:it-rex
ID: 36545370
it is an option,
0
 
LVL 11

Author Comment

by:it-rex
ID: 36545382
schwertner:,slightwv how plsql can do it?
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 total points
ID: 36545413
>>how plsql can do it?

Create a loop for the month numbers, use UTL_FILE.FOPEN to open a file using the generated number.

Then create a CURSOR loop for the select using the generated number for the month and use UTL_FILE.PUT_LINE to write out the values.

These functions are in the online docs.
0
 
LVL 11

Author Closing Comment

by:it-rex
ID: 36574642
thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

671 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question