If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Spool file?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-10-06, 07:08
zimon1972 zimon1972 is offline
Registered User
 
Join Date: Jan 2006
Posts: 9
Spool file?

Hi there,

I have a query that I would like to automate: not only the query itself but also the results. I think this can be done by creating a spool file?
Reply With Quote
  #2 (permalink)  
Old 01-10-06, 08:04
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Yes, query results can be spooled into a file.

But, until you tell us which DB you use, it is hard to tell the correct answer.
Reply With Quote
  #3 (permalink)  
Old 01-10-06, 08:17
zimon1972 zimon1972 is offline
Registered User
 
Join Date: Jan 2006
Posts: 9
Ok, I use Toad en SQL*PLus on Oracle databases.

What I would like, is the following:

I have already designed a query, that produces (on screen, not in a table) the number of customers per month.

I would like to automatically run this query without manually starting Toad and/or SQL*Plus and to save the results in a txt file on my c drive.
Reply With Quote
  #4 (permalink)  
Old 01-10-06, 08:49
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
To run the script automatically, use some kind of a scheduler on your computer (MS Windows, for example, offer "Scheduled tasks" under Accessories - System tools). I'm not sure can it be done through TOAD (but I'd bet NO if you ask me), but I'm sure it CAN be done in SQL*Plus.

Spooling in Oracle can be done using SPOOL command in SQL*Plus. A very simple SQL script example would be this:

spool output.txt
select * from emp;
spool off;
Reply With Quote
  #5 (permalink)  
Old 01-10-06, 09:18
zimon1972 zimon1972 is offline
Registered User
 
Join Date: Jan 2006
Posts: 9
Ok, so I can give my script - including the spool command - a name and save it as e.g. c:/test.sql.

How do I than schedule it via MS Windows? The wizard talks about the application to select (here,SQL Plus), but how does Windows know that it only shoudl run my c:test.sql??
Reply With Quote
  #6 (permalink)  
Old 01-10-06, 12:32
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
To run a .sql script, you will need another - batch (.bat) script. It should look like this:

SQLPLUS username/password @test.sql

This is SQL*Plus User's Guide and Reference; you might want to read it, especially Chapter 5: Using Scripts in SQL*Plus
Reply With Quote
  #7 (permalink)  
Old 01-11-06, 02:35
zimon1972 zimon1972 is offline
Registered User
 
Join Date: Jan 2006
Posts: 9
You have to help me here...

This is my query:
SET TERMOUT OFF;
SET ECHO OFF;
SPOOL c:/test.txt
select to_char(a.day_of_creation,'yyyy-mm') as month,count(distinct a.customer_accountno) as aantal
from ecl_cases a
where a.CASE_TYPE<>'BLOKSTORINGEN'
and (to_char(a.DAY_OF_CREATION,'yyyy-mm-dd')>='2004-10-01' and to_char(a.DAY_OF_CREATION,'yyyy-mm-dd')<='2004-12-31')
group by to_char(a.day_of_creation,'yyyy-mm')
order by to_char(a.day_of_creation,'yyyy-mm') asc;
SPOOL OFF;

I have saved this as test.sql

So, now I would like to only double-click on this file in Windows explorer to run it. So, without starting SQL*Plus myself. What should I do than?
Reply With Quote
  #8 (permalink)  
Old 01-11-06, 02:43
zimon1972 zimon1972 is offline
Registered User
 
Join Date: Jan 2006
Posts: 9
I tried your bat file option, but the MS-DOS screen gives:

ORA-12560: TNS: protocol adapter error

Do I have to put the database-name (server) aslo in the bat.file?
Reply With Quote
  #9 (permalink)  
Old 01-11-06, 04:09
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
For example, those two files might look like below. You would double click on P.BAT file to run P.SQL file and have output stored in P.TXT file.

REM P.SQL FILE
spool p.txt
select count(*) from tab;
spool off;
exit;

REM P.BAT FILE
sqplus -s username/password@database @p.sql
Reply With Quote
  #10 (permalink)  
Old 01-11-06, 04:23
zimon1972 zimon1972 is offline
Registered User
 
Join Date: Jan 2006
Posts: 9
Yep, it works! Great, thanx for all your help!
Reply With Quote
  #11 (permalink)  
Old 01-11-06, 07:16
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
No problem, I'm glad you've made it.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On