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
Solved

Using Spool to create a .csv

Posted on 2012-03-19
2
479 Views
Last Modified: 2012-03-19
I'm running the following in sqlplus to create a .csv:
set echo off
set verify off
set termout off
set heading off
set pages 50000
set feedback off
set newpage none
set linesize 200

spool C:\test\QueryOutput.csv 
select 'personid'
||','||'personnum'
||','||'personfullname'
||','||'lastnm'
||','||'firstnm'
||','||'employmentstatus'
||','||'companyhiredtm' 
from dual;
select to_char(personid) 
  ||','||to_char(personnum)
  ||','||rtrim(lastnm)
  ||','||rtrim(firstnm)  
  ||','||to_char(employmentstatus)
  ||','||to_char(companyhiredtm,'dd-mon-yyyy')
from vp_employeev42
Where personid < '500' and employmentstatus = 'Active';
spool off

Open in new window


The output is as follows:
SQL> select 'personid'
  2  ||','||'personnum'
  3  ||','||'personfullname'
  4  ||','||'lastnm'
  5  ||','||'firstnm'
  6  ||','||'employmentstatus'
  7  ||','||'companyhiredtm'
  8  from dual;
personid,personnum,personfullname,lastnm,firstnm,employmentstatus,companyhiredtm
SQL> select to_char(personid)
  2    ||','||to_char(personnum)
  3    ||','||rtrim(lastnm)
  4    ||','||rtrim(firstnm)
  5    ||','||to_char(employmentstatus)
  6    ||','||to_char(companyhiredtm,'dd-mon-yyyy')
  7  from vp_employeev42
  8  Where personid < '500' and employmentstatus = 'Active';
335,00012345,LastName1,FirstName1,Active,20-jan-1978
357,00012346,LastName2,FirstName2,Active,09-feb-1979

SQL> spool off

Open in new window


The problem is the select statements are being displayed in the output which I do not want.  The last line "SQL> spool off" is also being displayed which I don't want.  Is there a way to only output the result set from the select statements in the .csv file?
0
Comment
Question by:algoma
2 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 37738362
Do noy copy/paste the command.  Place them in a file with a .sql extension.

above the spool add:
set echo off
set trimspool on
set pages 0
set feedb off

select ...


Then from the sqlplsu prompt:  @whateverfile
0
 

Author Closing Comment

by:algoma
ID: 37738413
beautiful
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
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 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.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

856 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