Solved

Save Output of SQL Query in CSV file Using SQL *Plus

Posted on 2011-09-12
7
391 Views
Last Modified: 2012-06-27
Right Now I have done

sql > Spool xyz.txt
sql > <<my Query >>

output in file xyz.txt stored as

Column_Name1               Column_Name2                                                                                                
-------------------------               ---                                                  
ssss66666                             PNR                                                  
sdfgs6666                             PNR                                                  
sfdgfsdg6s                            PNR                                                  
sdfg326sd                             PNR                                                  
sdfg30713                            PNR                                                  
sdfgfsdfg                              PNR                                                  
sdfgsdfgs                             PNR                                                  
sdfgfgfds                             PNR                                                  
G22178                               PNR                                                  
G22402                               PNR                                                  

Column_Name1               Column_Name2                                                  
-------------------------                  ---                                              
sdfgsdfgsdf                              PNR                                                  
sfgfsdgfggf                               PNR                                                  
sdfgsdfgdf                                PNR    


Would like to have the output as a comma seperated values like

column1o/p, column2o/p, column1o/p, column2o/p, column1o/p, column2o/p, column1o/p, column2o/pcolumn1o/p, column2o/p, column1o/p, column2o/pcolumn1o/p, column2o/p, column1o/p, column2o/pcolumn1o/p, column2o/p, column1o/p, column2o/pcolumn1o/p, column2o/p, column1o/p, column2o/pcolumn1o/p, column2o/p, column1o/p, column2o/p............so on//

Please help me with such a query so that I can see the output as CSV file or also into an excel file where the entire value can be given as input to SQL Loader and load into another table in another schema




0
Comment
Question by:XxtremePro
  • 4
  • 3
7 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 36523555
you can use colsep   to  create csv,  but it won't concatenate all the values onto a single line
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 36523589
set colsep ,
select column1, column2 from yourtable
0
 

Author Comment

by:XxtremePro
ID: 36523778
good to have you back...sdstuber...

set echo off newpage 0 space 0 pagesize 0 feed off head off trimspool on
spool distinct_cont_iss.csv
select distinct column1 || ',' || nvl(trim(column2),'PNR')||','
  from contract;
 
spool off

Problem - Column1 defined as char(25 Byte) and data size is only 8 Byte

Giving output of '12345678                 '<< How to avoid the spaces and have output as 12345678,PNR,12345678,PNR,12345678,PNR,12345678,PNR... instead of 12345678                 ,PNR,12345678                 ,PNR,12345678                 ,PNR....

please help...........please

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:XxtremePro
ID: 36523820
I think the space is fine as the new table in which I would load this data has also the same space defined so no constraints

Thanks for your comment...
0
 

Author Comment

by:XxtremePro
ID: 36523829
Would like to know any tools that can help to assist me in loading this new CSV file apart from SQL Loader to load the data as I am not able to find SQL Loader.exe in C:\oracle\product\10.2.0\client_1\BIN

Please help
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 36523896
Oracle's free tool SQL Developer has a data loader

3rd party products PL/SQL Develoer and Toad have data loaders too


if your local client is incomplete, you can reinstall

http://www.oracle.com/technetwork/database/10201winsoft-095341.html
0
 

Author Closing Comment

by:XxtremePro
ID: 36600925
Solutions frm Experts Exchange always help...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

679 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