select data from oracle table and save the output as csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting select data from oracle table and save the output as csv file
# 1  
Old 11-05-2009
select data from oracle table and save the output as csv file

Hi

I need to execute a select statement in a solaris environment with oracle database. The select statement returns number of rows of data.

I need the data to be inserted into a CSV file with proper format. For that we normally use "You have to select all your columns as one big string, concatenating each column".
But my select query contains group by and order by functions using some alias names of the columns.
Can someone help me with the changes required to the sql query format to make sure that the data is inserted properly into csv file.

Code:
select
e.employee_name emp_name||'  ,'||
e.employee_address emp_add||'  ,'||
d.department_name dest_name

This is not working as there are some alias names for the columns.
# 2  
Old 11-05-2009
Better you can use the "set options" available with SQL*PLUS.

something like this :

Code:
set colsep=','

# 3  
Old 11-05-2009
I think you need something like this:

Code:
select emp_name||'  ,'|| emp_add||'  ,'|| dest_name
  from ( 
    select 
      e.employee_name emp_name,
      e.employee_address emp_add,
      d.department_name dest_name
      ...



---------- Post updated at 09:35 AM ---------- Previous update was at 09:26 AM ----------

Quote:
Originally Posted by panyam
Better you can use the "set options" available with SQL*PLUS.

something like this :

Code:
set colsep=','

With colsep the result will depend on linesize and the output could be different from what you expect:

Code:
SQL> set lines 132
SQL> set colsep ,
SQL> select ename name, customerid id from t;

NAME      ,        ID
----------,----------
name1     ,         1
name2     ,         2

SQL> select name||', '||id from (select ename name, customerid id from t);

NAME||','||ID
----------------------------------------------------
name1, 1
name2, 2

SQL> set lines 10
SQL> select ename name, customerid id from t;

NAME
----------
        ID
----------
name1
         1

name2
         2


SQL> select name||', '||id from (select ename name, customerid id from t);

NAME||','|
----------
name1, 1
name2, 2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Parsing - export html table data as .csv file?

Hi all, Is there any out there have a brilliant idea on how to export html table data as .csv or write to txt file with separated comma and also get the filename of link from every table and put one line per rows each table. Please see the attached html and PNG of what it looks like. ... (7 Replies)
Discussion started by: lxdorney
7 Replies

2. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

3. Shell Programming and Scripting

Save output of updated csv file as csv file itself

Hi, all I want to sort a csv file based on timestamp from oldest to newest and save the output as csv file itself. Here is an example of my csv file. test.csv SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0739.JPG,2015:02:17 11:32:21 /home/intannf/foto/IMG_0749.JPG,2015:02:17 11:37:28... (10 Replies)
Discussion started by: refrain
10 Replies

4. Shell Programming and Scripting

How to select the rows from oracle table using perl?

Hi, I am connecting to oracle database using perl. I am able to connect but i am not able to get all the rows from the table. Here is the code. #!/usr/bin/perl use DBI; my $dbh=DBI->connect("DBI:Oracle:student","class","welcome") or die "Couldnot connect oracle Database";... (1 Reply)
Discussion started by: vanitham
1 Replies

5. Shell Programming and Scripting

How to add data from 2 input files and save it in 1 output file

Hi, i have 2 input files which are file1.txt and file2.txt. I need to extract data from file1.txt and file2.txt and save it in file3.txt like example below:- File1.txt ID scrap1 Name scrap1 start 1 end 10 ID scrap2 Name scrap2 start 11 end ... (4 Replies)
Discussion started by: redse171
4 Replies

6. UNIX for Dummies Questions & Answers

Storing data from a table into a csv file

Hi I need to write a bash script to take the data stored in 3 oracle tables .. and filter them and store the results in a csv file. It is an Oracle database Thank you (1 Reply)
Discussion started by: ladyAnne
1 Replies

7. Shell Programming and Scripting

Data fetched from text file and save in a csv file

Hi i have wriiten a script which fetches the data from text file, and saves in the output in a text file itself, but i want that the output should save in different columns. I have the output like: For Channel:response_time__24.txt 1547 data points 0.339 0.299 0.448 0.581 7.380 ... (1 Reply)
Discussion started by: rohitkalia
1 Replies

8. Shell Programming and Scripting

load a data from text file into a oracle table

Hi all, I have a data like, 0,R001,2,D this wants to be loaded into a oracle database table. Pl let me know how this has to be done. Thanks in advance (2 Replies)
Discussion started by: raji35
2 Replies

9. UNIX for Advanced & Expert Users

How to load comma seperated values file (*.csv) into Oracle table

Hi all I need to input values in a .csv file into my Oracle table running in Unix, I wonder what would be the command to do so... The values are recorded in an excel file and I tried using a formatted text file to do so but failed because one of the field is simply too large to fit in the... (4 Replies)
Discussion started by: handynas
4 Replies

10. UNIX for Dummies Questions & Answers

How to load comma seperated values file (*.csv) into Oracle table

Hi all I need to input values in a .csv file into my Oracle table running in Unix, I wonder what would be the command to do so... The values are recorded in an excel file and I tried using a formatted text file to do so but failed because one of the field is simply too large to fit in the... (5 Replies)
Discussion started by: handynas
5 Replies
Login or Register to Ask a Question