Sql batch script to write multiple query results to excel/text files

I need to run several Oracle 11g  select queries and write the results to excel sheets

Example
select docname from TbDocs where plant = 'Plant1'   -   Write to Plant1.xlsx
select docname from TbDocs where plant = 'Plant2'   -   Write to Plant2.xlsx
select docname from TbDocs where plant = 'Plant3'   -   Write to Plant3.xlsx
.. has 20  plants total
I need this Each plant data going to separate excel files , due to some requiremnt
(NOTE:   It can be text files instead of excel files, If I can read it in excel later)
Can I have a batch sql script running and writing it to 20 Plant excel files in one shot?
Sam OZAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

johnsoneSenior Oracle DBACommented:
Why not a simple SQL script?  Run through SQL*Plus and spool each output.
set head off
set term off
set trimspool on
set lines 32767
set pages 0
set feed off
spool Plant1.xlsx
select docname from TbDocs where plant = 'Plant1';
spool off
spool Plant2.xlsx
select docname from TbDocs where plant = 'Plant2';
spool off
spool Plant3.xlsx
select docname from TbDocs where plant = 'Plant3';
spool off
exit

Open in new window

That can be scripted, or run interactively.  You may also need to add more set commands to get the output the way you want it, but that should get you most of the way there.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sam OZAuthor Commented:
thanks. What location will the excel files  get saved?
0
johnsoneSenior Oracle DBACommented:
They would be saved in the directory you run the script from.

You can qualify the spool commands with a full path to put them wherever you want.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.