Solved

Multiple spool files in a single process

Posted on 2011-09-07
8
618 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 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 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
Independent Software Vendors: 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 45

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 45

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 45

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

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!

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

717 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