Solved

Using Spool to create a .csv

Posted on 2012-03-19
2
474 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.

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…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

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

12 Experts available now in Live!

Get 1:1 Help Now