Output the SQL Query result to a File


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Output the SQL Query result to a File
# 1  
Old 11-11-2010
Output the SQL Query result to a File

Hello Guys,

This message is somewhat relates with last thread. But I need to re-write thing. I start over a little. I am stuck now and need your help.

Here is my script-
Code:
#! /bin/ksh
export ORACLE_HOME=/opt/oracle/app/oracle/product/9.2
/opt/oracle/app/oracle/product/9.2/bin/sqlplus -s ccare/ccare@ccbs <<EOF
@myquery.sql

EOF

Myquery.sql output a COUNT of some raws. I just wanted to input those count (only numbers) to a file. And every time I run it should append in the file.

Your help pls.

Last edited by vbe; 11-11-2010 at 11:03 AM.. Reason: Code tags!
# 2  
Old 11-11-2010
Would setting "spool" to a list file in your sql script not suffice?
Then of course you would have to extract what you want to append elsewhere...
# 3  
Old 11-11-2010
Hi VBE,

can you please show me exact way.

Much appreciated..
# 4  
Old 11-11-2010
What is in your sql script?

---------- Post updated at 16:58 ---------- Previous update was at 16:55 ----------

e.g. at the beginning of script
Code:
spool /tmp/my_output.lst
set heading off
set feedback 0
set verify off
set auto off
set echo off


here is the sql request....
/

spool off
!echo "Spool file generated..."
clear buffer
EXIT

# 5  
Old 11-12-2010
Hi VBE,
Quote:
Originally Posted by vbe
What is in your sql script?
it is count statement in SQL script.
select count(*) from abc where xyz<>2;

---------- Post updated at 07:05 AM ---------- Previous update was at 06:43 AM ----------

Quote:
Originally Posted by vbe
What is in your sql script?

---------- Post updated at 16:58 ---------- Previous update was at 16:55 ----------

e.g. at the beginning of script
Code:
spool /tmp/my_output.lst
set heading off
set feedback 0
set verify off
set auto off
set echo off


here is the sql request....
/

spool off
!echo "Spool file generated..."
clear buffer
EXIT

VBE, I tried your script it is executed and working almost. Here is my script that i ran-
#! /bin/ksh
export ORACLE_HOME=/opt/oracle/app/oracle/product/9.2
/opt/oracle/app/oracle/product/9.2/bin/sqlplus -s abc/abc@mydb <<EOF
spool /mycount.txt
set heading off
set feedback 0
set verify off
set auto off
set echo off

@myquery.sql
spool off
!echo "Spool file generated..."
clear buffer
EXIT

EOF



Here was the output
[aix2][/]# ./myquery.sh

0
Spool file generated...

[aix2][/]# cat mycount.txt

0
[aix2][/]#


I plan to put he script in crontab to run every 10 minutes. And hence I need the sql output in same file like below:
Date Run count
2010-11-12 06:00 0
2010-11-12 06:10 10
2010-11-12 06:20 3


Please say how can I do it??

---------- Post updated at 11:36 AM ---------- Previous update was at 07:05 AM ----------

Hi Guys,

I need to spool the results in same file just after 10 minutes. I will put that script in crontab;

How to modify the script accordingly?
# 6  
Old 11-12-2010
Quote:
Originally Posted by thepurple
...Here is my script that i ran-
#! /bin/ksh
export ORACLE_HOME=/opt/oracle/app/oracle/product/9.2
/opt/oracle/app/oracle/product/9.2/bin/sqlplus -s abc/abc@mydb <<EOF
spool /mycount.txt
set heading off
set feedback 0
set verify off
set auto off
set echo off

@myquery.sql
spool off
!echo "Spool file generated..."
clear buffer
EXIT

EOF
...
You may want to try the OS's redirection capability here, instead of spool -

Code:
...
/opt/oracle/app/oracle/product/9.2/bin/sqlplus -s abc/abc@mydb <<EOF >> /mycount.txt
...

Unfortunately, you are still on Oracle 9i, which is an old version.

From Oracle 10g Release 2 onwards, the spool command comes with an "append" option that appends data to a spool file.

Prior to that version, the spool command creates the spool file every time it is invoked.

HTH,
tyler_durden

Last edited by durden_tyler; 11-12-2010 at 03:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Not able to write SQL query output in to .csv file with shell script.

I am trying to write SQL query output into a .csv file. But in the output columns are displaying in different lines instead of coming in one line. Main Code shell script: this is my code: #!/bin/bash file="db_detail.txt" . $file rm /batch/corpplan/bin/dan.csv... (6 Replies)
Discussion started by: sandeepgoli53
6 Replies

2. Programming

Oracle simple SQL query result in: ORA-08103: object no longer exists

Dear community, please help with a query on Oracle. I'm using SQLPlus (but with SQLDeveloper is the same) to accamplish a sinple query like: select count(*) from ARCHIT_D_TB where (TYP_ID=22 OR TYP_ID=23) and SUB_TM like '%SEP%' and CONS=1234This is a very simple query that works perfect until... (5 Replies)
Discussion started by: Lord Spectre
5 Replies

3. Shell Programming and Scripting

Get SQL query result to file in putty

How to Get SQL query result to file in putty? I have one SQL query and I want that query output to be redirected to the file. uname -a SunOS XXX 5.8 Generic_117350-58 sun4u sparc SUNW,Sun-Fire-480R Please suggest. (7 Replies)
Discussion started by: pamu
7 Replies

4. Shell Programming and Scripting

SQL query in UNIX script - output in flat file

Hi, I never did this before... what I want to do is execute a SQL query from a unix script and redirect sql query's output to a flat file (comma separated one) without the header info (no column names). I would also want not to print the query's output to the screen. snapshot of my script:... (13 Replies)
Discussion started by: juzz4fun
13 Replies

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

6. Shell Programming and Scripting

redirecting sql query output to a file

Hi, I am executing sql files in my unix shell script. Now i want to find whether its a success or a failure record and redirect the success or failure to the respective files. meaning. success records to success.log file failure record to failure.log file. As of now i am doing like... (1 Reply)
Discussion started by: sailaja_80
1 Replies

7. Shell Programming and Scripting

How to Format the result driven from a SQL Query

Hi All, I want to format the result driven from the query into neat format. For example pls find the below code, #! /bin/sh result=' sqlplus -s uname/passwrd@DBname select no,name,address,ph_no, passport_no,salary,designation from emp_table where salary>1000; exit EOF' ... (8 Replies)
Discussion started by: little_wonder
8 Replies

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

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

10. Shell Programming and Scripting

any possible to run sql result and output to file

Hi, I search all post...and no soluation about..if i would like to run a sql statement and output the result to txt file. for example, i usually run "sql" to logon the database and run select statement. Then I need to copy the output into the result.txt. Can I run the script to do this... (7 Replies)
Discussion started by: happyv
7 Replies
Login or Register to Ask a Question