Solved

Multiple spool files in a single process

Posted on 2011-09-07
8
607 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)
Comment Utility
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
Comment Utility
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)
Comment Utility
>>'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
Comment Utility
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
Comment Utility
>>'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)
Comment Utility
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
Comment Utility
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)
Comment Utility
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.

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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…
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

763 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

5 Experts available now in Live!

Get 1:1 Help Now