Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-23-2006
Registered User
 

Join Date: Mar 2006
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
piping oracle output to a file?

Hi All...

Does anyone know how to pipe the output of a "select" statement from a call to Oracle to a file?

ANy ideas woule be greatly appreciated!

Code is as below...

echo "producing CSV file 2..."
sqlplus -s $username/$password@$database<<EOF

set serveroutput on size 1000000
set verify off
set feedback off
set pagesize 0
VARIABLE vi_err NUMBER

SELECT empno||','||ename||','||job||','||mgr||hiredate||','||sal||','||comm||','||deptno FROM emp;
EXIT:vi_err
EOF
Sponsored Links
    #2  
Old 07-24-2006
Registered User
 

Join Date: Oct 2005
Location: Chennai
Posts: 370
Thanks: 0
Thanked 3 Times in 3 Posts
spool

Why dont you spool the output to a file

Code:
set serveroutput on size 1000000
set verify off
set feedback off
set pagesize 0
spool filename
VARIABLE vi_err NUMBER

SELECT empno||','||ename||','||job||','||mgr||hiredate||','||sal||','||comm||','||deptno FROM emp;
spool off
EXIT:vi_err
EOF

Sponsored Links
    #3  
Old 07-24-2006
Dhruva's Avatar
Registered User
 

Join Date: Mar 2006
Location: India
Posts: 255
Thanks: 0
Thanked 1 Time in 1 Post
If you want to use redirection to a file ..try this.Now testing.dat will have the sql output.

X=`sqlplus -s user/pwd@host<<eof
set serveroutput on;
set feedback off;
set linesize 1000;
select * from table where rownum<5;
EXIT;
eof`

echo $X>testing.dat
    #4  
Old 07-24-2006
Registered User
 

Join Date: Mar 2006
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks!

Cheers lads! v useful tips.

I also found another way... as below.

{
echo "set pagesize 0"
echo "SELECT empno||','||ename||','||job||','||mgr||hiredate||','||sal||','||comm||','||deptno "\
"FROM emp;"
} | sqlplus -s $username/$password@$database >> $root_dir/$csv_file1

Kind Regards

Satnam
Sponsored Links
    #5  
Old 07-28-2006
Registered User
 

Join Date: Jul 2006
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
hi satnam.. is there a way not to mention the user name and pasword in the script. i have a requirement like this. i have to run a sql query using shell scripts. once the query is run, the results have to be stored in a data file. if any values are returned by the query, it should display an error msg to the user or else it has to exit. pls help me with this as i am totally new to shell scripting and i just started to learn..
Sponsored Links
    #6  
Old 05-04-2007
Registered User
 

Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Please explain this code snippet to me

Hi all,
I am new to shell scripting and want to know how this code works.

{
echo "set pagesize 0"
echo "SELECT empno||','||ename||','||job||','||mgr||hiredate||','||sal||','||comm||','||deptno "\
"FROM emp;"
} | sqlplus -s $username/$password@$database >> $root_dir/$csv_file1

Thanks in advance.
Sponsored Links
    #7  
Old 05-04-2007
Registered User
 

Join Date: Jan 2005
Posts: 683
Thanks: 0
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by samuelc
Hi all,
I am new to shell scripting and want to know how this code works.

{
echo "set pagesize 0"
echo "SELECT empno||','||ename||','||job||','||mgr||hiredate||','||sal||','||comm||','||deptno "\
"FROM emp;"
} | sqlplus -s $username/$password@$database >> $root_dir/$csv_file1

Thanks in advance.

Code:
{
  echo "...
} | sqlplus

basically collects all of the "echo" command's output as a group and they are piped through to Oracle's sqlplus command processor. So it behaves just like running sqlplus interactively and you typing in those commands.


Code:
>> $root_dir/$csv_file1

directs the output from the SQL commands to a file on the UNIX server. The path and filename are in variables $root_dir and $csv_file.

Ultimately, the SQL is building a csv (comma delimited) file that will be used for, presumably, as an input to another system.

Thomas
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Piping output of "top" to a text file safraser UNIX for Dummies Questions & Answers 13 05-21-2009 04:13 PM
piping output of tail running in background anuramdas Shell Programming and Scripting 4 07-27-2007 02:20 PM
piping output to echo A1977 Shell Programming and Scripting 3 11-01-2006 07:58 AM
piping the output of find command to grep 435 Gavea UNIX for Dummies Questions & Answers 1 10-05-2006 04:48 PM
Piping output to while read Ultimodiablo Shell Programming and Scripting 3 11-26-2005 09:38 PM



All times are GMT -4. The time now is 05:05 AM.