Solved

sql*plus syntax

Posted on 2014-02-12
10
571 Views
Last Modified: 2014-02-12
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.
0
Comment
Question by:pma111
  • 5
  • 4
10 Comments
 
LVL 20

Accepted Solution

by:
Amitkumar Panchal earned 250 total points
ID: 39852917
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>
sql_001;

spool <path_to_file_002.csv>
sql_002;

spool <path_to_file_003.csv>
sql_003;

and

so on

Open in new window


Hope this helps you.
0
 
LVL 28

Assisted Solution

by:Naveen Kumar
Naveen Kumar earned 250 total points
ID: 39852918
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.

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

then from sql prompt, execute it
SQL>@test.sql

Thanks,
0
 
LVL 3

Author Comment

by:pma111
ID: 39852923
thanks
0
 
LVL 3

Author Comment

by:pma111
ID: 39852936
is "spool off" required after every query, as the top example didnt include spool off?

also what does || ',' || represent?
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39852942
|| ',' || - 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.

Thanks,
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

by:pma111
ID: 39852948
what represents the end of a query so it moves on to the next, is that the ; character?
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39852958
yes
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39852962
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

Thanks
0
 
LVL 3

Author Comment

by:pma111
ID: 39852970
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)
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39852996
yeah.. you can leave a blank line as well for the sql query but it needs to have ; at th end
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Creating and Managing Databases with phpMyAdmin in cPanel.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

744 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

11 Experts available now in Live!

Get 1:1 Help Now