?
Solved

Multiple spool files in a single process

Posted on 2011-09-07
8
Medium Priority
?
628 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:Kent Olsen
[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
  • 5
  • 3
8 Comments
 
LVL 77

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 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 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 77

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Author Comment

by:Kent Olsen
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 46

Author Comment

by:Kent Olsen
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 77

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 46

Author Closing Comment

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

  :^}
0
 
LVL 77

Expert Comment

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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.  …
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

765 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