Export SQL results to .TXT file for emailing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Export SQL results to .TXT file for emailing
# 1  
Old 08-24-2010
Export SQL results to .TXT file for emailing

Hi everyone,
I am new to unix and bash and in need of some help.
I am writing a script that will execute a SQL query. The script runs and the SQl query runs, but I cannot figure out how to save the results as a file that can be emailed to a user. Here is my scripts thus far:
Code:
#!/bin/sh
SID=$1
DIR=/u01/test/testcomn/admin/scripts/requestedqueries/
ORACLE_SID=$SID; export ORACLE_SID
ORACLE_HOME directories to be searched by the .sql statement
LD_LIBRARY_PATH
PATH=$ORACLE_HOME/bin:$PATH; export PATH
cd $DIR

sqlplus user/PWD @export_icat.sql
echo 'Today's Results'  | mail -s "Results" user@domain.com


Thanks!


Moderator's Comments:
Mod Comment Please use code tags, thank you!

Last edited by Franklin52; 08-24-2010 at 01:34 PM..
# 2  
Old 08-24-2010
Hi!
I'm not really familiar with Oracle. Is the file export_icat.sql a file containing commands for sqlplus? Where does the output go? To the screen (stdout)?
Then You can probably redirect it to a file, let's call it export_icat.out.

Your mail command sends a mail to user@domain.com, with the Subject "Results" and with the body content "Today's Results". Maybe not even that, depending on what the quotes are. If You want to send the content of the text file export_icat.out as the mail body, use:

Code:
...
sqlplus user/PWD @export_icat.sql > export_icat.out
mail -s "Results" user@domain.com < export_icat.out
...

Best regards,
Lakris

PS There are other commands in Your script that won't work as it is written.
Maybe
Code:
ORACLE_HOME directories to be searched by the .sql statement
LD_LIBRARY_PATH

should be replaced with something that actually sets these variables?

Last edited by Lakris; 08-24-2010 at 02:58 PM.. Reason: PS...
# 3  
Old 08-24-2010
That worked exactly like I needed it to.

As for the two lines that should not have been there, they were actually commented out and I failed to delete them when I was pasting the code in the question.
Thanks SO much!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Emailing results of a pl sql procedure from UNIX shell script

Hello All, I am writing the below unix script to email the result of a small pl sql procedure: #!/bin/bash ORACLE_HOME=/opt/oracle/orcts/product/9.2.0; export ORACLE_HOME SQLPLUS=$ORACLE_HOME/bin/sqlplus sqlplus -s user/pass@Db_instance<<EOF set echo off set feedback off set pages 0... (9 Replies)
Discussion started by: Bunty bedi
9 Replies

2. Shell Programming and Scripting

shell scripting to export the results to an excel file

Hi, I am facing issues while exporting the results retrieved from the Database to an excel file. I have 5 count query in order to count the records in 5 different tables. I had connected to the DB and retrieved the column count and printed in the screen. But wat I need to do is, I... (8 Replies)
Discussion started by: Learner 20
8 Replies

3. Shell Programming and Scripting

How to store the sql query output into txt file?

Hi I want ot save SQL query result in one txt file. for that i have written one code line sqlplus -s $dbstring @/usr/local/bin/sched/nightly_Cronjob/exec_123.sql >> /usr/local/bin/sched/nightly_Cronjob/result.txt but it is not working . database : Oracle so please advice me how can i... (7 Replies)
Discussion started by: Himanshu_soni
7 Replies

4. Shell Programming and Scripting

Writing sql results to file using ksh -nevermind

I'm having problems with writing my sql results to a file: sqlplus -S username/password@DB <<!! set echo off set verify off set showmode off set feedback off set timing off set linesize 250 set wrap off set pagesize 0 set newpage none set tab off set trimspool on set colsep... (1 Reply)
Discussion started by: avillanueva
1 Replies

5. Shell Programming and Scripting

Export data from DB2 table to .txt file(space delimited)

Hi I need help on this. Its very urgent for me.. please try to help me out.. I have data in tables in DB2 database. I would like to export the data from DB2 tables into a text file, which has to be space delimited. so that I can carry out awk, grep operations on that file. I tried to export... (2 Replies)
Discussion started by: ss3944
2 Replies

6. UNIX for Dummies Questions & Answers

Backing Up and Emailing Home Directory and SQL Databases

Hello, I run a web hosting company, and I'm wondering how I can use cPanel's Cron Jobs so that a copy of my entire home directory and a copy all of my MySQL databases can be compressed and emailed to me. I know nothing about Linux, Unix, or whatever that thing with the penguin is called. :) ... (7 Replies)
Discussion started by: millipedeman
7 Replies

7. UNIX for Dummies Questions & Answers

store SQL statements and results in a file

Hello Guys... I want a small help from you guys. Actually in Oracle, we are having a utlity called spool through which can store whatever SQL statements executed and other queries and the output of those queries in a file So, similarly in Unix, if I start a session executing a number of Unix... (2 Replies)
Discussion started by: mraghunandanan
2 Replies

8. Shell Programming and Scripting

How to export a unix file to txt file?

Is there any way by which u can export a file in unix box to a notepad or txt file?:confused: (3 Replies)
Discussion started by: nohup
3 Replies

9. Shell Programming and Scripting

Redirecting sql select query result to txt file

Hi Yogesh, Lucky that i caught you online. Yeah i read about DBI and the WriteExcel module. But the server is not supporting these modules. It said..."Cannot locate DBI"..."Cannot locate Spreadsheet::WriteExcel" I tried creating a simple text file to get the query output, but the... (1 Reply)
Discussion started by: dolphin123
1 Replies

10. Shell Programming and Scripting

Redirecting sql select query result to txt file

Hi , I just found you while surfing for the string 'Redirecting sql select query output from within a shell script to txt file/excel file' Could you find time sending me the code for the above question? It'll be great help for me. I have a perl file that calls the sql file... (1 Reply)
Discussion started by: dolphin123
1 Replies
Login or Register to Ask a Question