Solved

Multiple spool files in a single process

Posted on 2011-09-07
8
608 Views
Last Modified: 2012-05-12

I've got a need to generate dozens, perhaps hundreds of identical reports from a single data source.  (Identical in format, not content.)

What I'd like to do is write a single script that will execute the same query repeatedly with a slightly different filter and spool the output to different file names.  Pseudo-code is below:

  BEGIN
    FOR cid IN 1..100
    LOOP
      SPOOL c:\temp\data_file_&cid
      select item_list INTO C FROM c_prrd where coalition_id = cid;
    END LOOP;
  END;

I can seem to find a "reasonable" solution.  :(

Kent


0
Comment
Question by:Kdo
  • 5
  • 3
8 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36498370
spool is a sqlplus command not a PL/SQL command.

As soon as you add BEGIN and END, it is PL/SQL.

You can always use UTL_FILE to open the files and write the values.
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 36498393
I suppose you could have SQL write SQL or you.

Something like this (untested, just typed in):
---------------------
set lines 100
set trimspool on
set timing off
set feedback off

spool mytest.sql

BEGIN
    FOR cid IN 1..100
    LOOP
      dbms_output.put_line('SPOOL c:\temp\data_file_' || cid);
      dbms_output.put_line('select item_list INTO C FROM c_prrd where coalition_id = ' || cid);
     dbms_output.put_line('spool off');
    END LOOP;
  END;
/

@mytest.sql
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36498402
>>'select item_list INTO C

Not sure where 'C' is defined/declared so this might be an issue.
0
 
LVL 45

Author Comment

by:Kdo
ID: 36498411
Yeah, it is chicken and egg.  Can't create the loop except inside a block and can't execute the spool statement except outside the block.

The right way is to chase cursors and write the results with the UTL_FILE utility, but it sure would be "easier" if sqlplus and PL/SQL were more on speaking terms.


Kent
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 45

Author Comment

by:Kdo
ID: 36498438
>>'select item_list INTO C

Not sure where 'C' is defined/declared so this might be an issue.


Oops.  :)  Copied one too many things from "just another experiment gone awry".

I do kind of like the meta-sql approach, but I generally despise writing these temporary files as it just adds headaches to the next guy that might have his hands in this.  Still, it's shorter and cleaner than chasing cursors.


Kent
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36498824
Maybe some other Expert will arrive later and have some tricks that I'm unaware of.

That way we both get some new tricks!
0
 
LVL 45

Author Closing Comment

by:Kdo
ID: 36562975
Sorry for not closing this right off, but I was busy using the results of the answer.

  :^}
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36562988
No problem.  Glad you were able to use one of the methods.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query of Oracle 10g database. 8 76
automatic email alert 1 41
passing parameters to sql script oracle 4 26
Shredding xml into an oracle 11g Database 2 24
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

919 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now