sql*plus syntax

I have a list of SQL commands (to query an oracle database) in a text file, I would like to run them in batch via sql*plus. I want each query to write the results to an individual HTML or CSV file, how can this be done?

How do I need to seperate the queries in the text file , and how can I add a filename for the output to csv, html?

Advice most welcome.
Who is Participating?
Amitkumar PConnect With a Mentor Sr. ConsultantCommented:
you can use spool command for each of the queries in your batch. You will need to specify path to the file where you want to spool the result and then need to execute the query.

Refer more on spooling at http://www.dba-oracle.com/t_sqlplus_spool.htm

Then write an sql as given template.

spool <path_to_file_001.csv>

spool <path_to_file_002.csv>

spool <path_to_file_003.csv>


so on

Open in new window

Hope this helps you.
Naveen KumarConnect With a Mentor Production Manager / Application Support ManagerCommented:
prepare the queries accordingly to generate the csv output and put it as shown in the into a sql script which you can then execute.

spool emp.csv
select name || ',' || deptno from emp;
spool off

then from sql prompt, execute it

pma111Author Commented:
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

pma111Author Commented:
is "spool off" required after every query, as the top example didnt include spool off?

also what does || ',' || represent?
Naveen KumarProduction Manager / Application Support ManagerCommented:
|| ',' || - is to concate output from 2 columns using a comma delimiter - just an example for you to understand.

not for every query but if you have 5 queries and if the output needed to go to 5 different csv files then it can be used.

or even without using it, if you start another spool command with a different file name then also it works.

pma111Author Commented:
what represents the end of a query so it moves on to the next, is that the ; character?
Naveen KumarProduction Manager / Application Support ManagerCommented:
Naveen KumarProduction Manager / Application Support ManagerCommented:
for sql queries like select, update, delete , insert etc.. you need ; at the end

But for sql*plus commands like spool, column , heading etc you do not need ; at the end and they take new line as the end

pma111Author Commented:
thats interesting, so you need to write your sql in one block then, as some people add line breaks after the various clauses (i.e. select, from, where)
Naveen KumarProduction Manager / Application Support ManagerCommented:
yeah.. you can leave a blank line as well for the sql query but it needs to have ; at th end
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.

All Courses

From novice to tech pro — start learning today.