Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
select only modified column name  based on the status 15 55
dates - loop 12 65
Oracle 12c database link between pdb not working 20 72
Select and Insert Query running slow 4 32
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious sideā€¦
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

786 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