Spool - Append and Calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spool - Append and Calculation
# 1  
Old 03-05-2013
Spool - Append and Calculation

I am running a cron job, every 6 hours a day (6 AM, 12 PM, 6 PM, and 12 AM),

Code:
#!/usr/bin/ksh
......
$SQLPLUS / as sysdba <<!
@/home/oracle/scripts/daily.sql
!
cat /home/oracle/scripts/dbsizedaily.lst | mail -s "$TODAY: PROD DB Size" $RECIPIENTS

"@/home/oracle/scripts/daily.sql" has

Code:
spool /home/oracle/scripts/dbsizedaily append

select sum(bytes)/1024/1024/1024 "TOTAL_GB"
from dba_data_files;

select sum(bytes)/1024/1024/1024 "USED_GB"
from dba_segments;  

spool off
exit

and it simply generates

Code:
  TOTAL_GB                                                                      
----------                                                                      
3151.24316                                                                      


   USED_GB                                                                      
----------                                                                      
2330.40381              

  TOTAL_GB                                                                      
----------                                                                      
3151.24316                                                                      


   USED_GB                                                                      
----------                                                                      
2347.41551       

  TOTAL_GB                                                                      
----------                                                                      
3151.24316                                                                      


   USED_GB                                                                      
----------                                                                      
2349.76498

What I want to have out of the "@/home/oracle/scripts/daily.sql" is a tab delimeter file (to import into Excel better):

Code:
DATE         TIME           TOTAL_GB      USED_GB      %USED
3/5/2013    06:00 AM     3151.24316    2330.40381   73.95
3/5/2013    12:00 PM     3151.24316    2347.41551   74.49
3/5/2013    06:00 PM     3151.24316    2349.76498   74.56
.....

Please advise how to modify "@/home/oracle/scripts/daily.sql" or the ksh to get the output I want. If I can re-direct the output of spool /home/oracle/scripts/dbsizedaily append to another file, that works for me.

Last edited by vbe; 03-05-2013 at 08:48 AM..
# 2  
Old 03-05-2013
Join two tables and generate the result:
Code:
select to_char(sysdate, 'mm/dd/yyy') "DATE", to_char(sysdate, 'HH24:MI AM') "TIME", 
sum(a.bytes)/1024/1024/1024 "TOTAL_GB", sum(b.bytes)/1024/1024/1024 "USED_GB"
from dba_data_files a, dba_segments b;

For percentage: add another field, apply your math on a.bytes and b.bytes
These 2 Users Gave Thanks to Yoda For This Post:
# 3  
Old 03-05-2013
Quote:
Originally Posted by bipinajith
Join two tables and generate the result:
Code:
select to_char(sysdate, 'mm/dd/yyy') "DATE", to_char(sysdate, 'HH24:MI AM') "TIME", 
sum(a.bytes)/1024/1024/1024 "TOTAL_GB", sum(b.bytes)/1024/1024/1024 "USED_GB"
from dba_data_files a, dba_segments b;

For percentage: add another field, apply your math on a.bytes and b.bytes
If this combined query gives me the accurate result, it will be great, it doesn't..
HTML Code:
select to_char(sysdate, 'mm/dd/yyy') "DATE", to_char(sysdate, 'HH24:MI AM') "TIME", 
sum(a.bytes)/1024/1024/1024 "TOTAL_GB", sum(b.bytes)/1024/1024/1024 "USED_GB"
from dba_data_files a, dba_segments b
where a.TABLESPACE_NAME = b.TABLESPACE_NAME
# 4  
Old 03-05-2013
Fantastic input from bipinajith

Would this be better?

Code:
select a.tablespace_name,to_char(sysdate, 'mm/dd/yyy') "DATE", to_char(sysdate,
'HH24:MI AM') "TIME",
sum(a.bytes)/1024/1024/1024 "TOTAL_GB", sum(b.bytes)/1024/1024/1024 "USED_GB"
from dba_data_files a, dba_segments b
where a.TABLESPACE_NAME = b.TABLESPACE_NAME
group by a.tablespace_name

Is there a reason for the time to be both 24-Hour and have the AM/PM label?


Robin
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 03-05-2013
It gives me long details rather the the sum, so it doesn't work for me.

1)
PHP Code:
select to_char(sysdate'mm/dd/yyy'"DATE"to_char(sysdate'HH24:MI AM'"TIME"sum(bytes)/1024/1024/1024 "TOTAL_GB"
from dba_data_files 
2)
PHP Code:

select sum
(bytes)/1024/1024/1024 "USED_GB"
from dba_segments 
With scripts, if we put the output of Query 2 right next to Query 1, then that will be the perfect output.

It seems hard to get the output through a single query.

AM/PM is not needed with 24 Hour Format.

Please advise.
# 6  
Old 03-05-2013
How about using a PL/SQL?

PHP Code:
SET SERVEROUTPUT ON;
DECLARE
        
V_DATE          VARCHAR2(10);
        
V_TIME          VARCHAR2(10);
        
V_TOTAL_GB      dba_data_files.bytes%type;
        
V_USED_GB       dba_data_files.bytes%type;
        
V_USED_PER      dba_data_files.bytes%type;
BEGIN
        select to_char
(sysdate'mm/dd/yyyy'into V_DATE from dual;
        
select to_char(sysdate'HH24:MI'into V_TIME from dual;
        
select round(sum(bytes)/1024/1024/1024,4into V_TOTAL_GB from dba_data_files;
        
select round(sum(bytes)/1024/1024/1024,4into V_USED_GB from dba_segments;
        
select round((V_USED_GB 100)/V_TOTAL_GB,4into V_USED_PER from dual;
        
dbms_output.put_line (V_DATE || ' ' ||  V_TIME || ' ' ||  V_TOTAL_GB || ' ' || V_USED_GB || ' ' || V_USED_PER);
END;

Create a CSV instead, so that you can distinguish the fields and read it in a script:

PHP Code:
dbms_output.put_line (V_DATE || ',' ||  V_TIME || ',' ||  V_TOTAL_GB || ',' || V_USED_GB || ',' || V_USED_PER); 
Also turn off the FEEDBACK:

PHP Code:
SET SERVEROUTPUT ON FEEDBACK OFF
These 2 Users Gave Thanks to Yoda For This Post:
# 7  
Old 03-05-2013
That is really great! Appreciate it!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append date to sql*plus spool (log) file in shell script

SQL*Plus version : 11.2.0.4 OS : Oracle Linux 6.5 SQL*Plus is a client application to connect to oracle database. The log file for this tool is generated via spool command as shown below. I am trying to append date ( $dateString ) to spool file as shown below. $ cat test2.sh #!/bin/bash... (4 Replies)
Discussion started by: kraljic
4 Replies

2. Shell Programming and Scripting

Issues with SPOOL

I am using a unix shell script to create and then FTP the file to a mainframe application. The unix script uses spool to extract data from a Oracle database. The data is extracted into a text file. The text file is having all the data I need but not in the correct format. The rows of... (1 Reply)
Discussion started by: rameez
1 Replies

3. UNIX for Dummies Questions & Answers

Issue with SPOOL

Hi, I use SunOS and i connect to database via isql. When tried using SPOOL command to extract the report from database, system thrown me saying "Spoool command not found". Please advise. (3 Replies)
Discussion started by: kusathy
3 Replies

4. UNIX for Dummies Questions & Answers

Like spool in UNIX

Hi, Can you please help me how to spool the unix commands executed in my client and its output to a file in local machine. Basically, I want to save all OS commands executed in my session and its output into a file from my client something like we do oracle with "SPOOL". Thanks in advance.... (2 Replies)
Discussion started by: pointers
2 Replies

5. Solaris

Spool directory

hi all, I have unix box I install 2 zone on it I want to make spool directory and assign one to each zone How can I do that ? (5 Replies)
Discussion started by: coxmanchester
5 Replies

6. UNIX for Dummies Questions & Answers

Spool command in Unix

Hi, Is there any command in unix similar to spool command in oracle. (6 Replies)
Discussion started by: manosubsulo
6 Replies

7. Shell Programming and Scripting

Spool / Spool off

Hi everyone I'm new in this forum, I have a question: I have a script with many sql staments and I have the spool command at the beginning and the 'spool off' at the end of the script. But, I want to write after each sql stament to a log file not at the end. How can I do it? Thanks (0 Replies)
Discussion started by: edzela
0 Replies

8. UNIX for Dummies Questions & Answers

Spool Issue

Hi: At every month end I would like to collect the information of a spool. Every Spool has more then one job, below is the individual job information, I really want to collect into a text file. Sp-id Proc User Printer Id Pty Form Copy Total SP7494 MONTHEND SYED R123 P2 1 WIDE ... (3 Replies)
Discussion started by: shah2
3 Replies

9. UNIX for Dummies Questions & Answers

Spool

hi every body, is there any possibles to spool the commads with output(like oracle spool file) (1 Reply)
Discussion started by: kskumar
1 Replies

10. UNIX for Dummies Questions & Answers

Spool for Printer

i have created a new printer in the printcab file. when i use lpc and the status command it says: LMAPFE20: printer is on remote host lpt1: queuing is enabled printing is enabled cannot examine spool directory What ive... (1 Reply)
Discussion started by: JuergenW
1 Replies
Login or Register to Ask a Question