Solved

Multiple spool files in a single process

Posted on 2011-09-07
8
610 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
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: 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
 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Encryption Decryption in Oracle 12 117
Error executing command from server 6 49
Determine Who is Runnig my Bash Shell Script 4 79
Repeat query 13 46
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
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

813 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

12 Experts available now in Live!

Get 1:1 Help Now