Solved

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

Posted on 2011-09-12
7
386 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 73

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 73

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
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.

 

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 73

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

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

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
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…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

760 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

24 Experts available now in Live!

Get 1:1 Help Now