Include heading in text file
Hi Experts,
I need your HELP to include headings START_DATE, NUM_LOGS,MBYES,RSIZE in my text file "redo_history.log" below.
05-3-2009,36, 3600,100
05-4-2009,191, 19100,100
05-5-2009,56, 5600,100
06-1-2009,220, 22000,100
06-2-2009,245, 24500,100
06-3-2009,217, 21700,100
My desired output text file (redo_history.log) should be on below:
START_DATE, NUM_LOGS,MBYES,RSIZE
05-3-2009,36, 3600,100
05-4-2009,191, 19100,100
05-5-2009,56, 5600,100
06-1-2009,220, 22000,100
06-2-2009,245, 24500,100
06-3-2009,217, 21700,100
Im executing the following script below to generate text file named redo_history.log:
select dump_csv('SELECT Start_Date,
Num_Logs,
to_char(Round(Num_Logs * (Vl.Bytes / (1024 * 1024)),2),''999999999'') AS Mbytes,
Vl.Bytes / (1024*1024) AS RSize
FROM (SELECT To_Char(Vlh.First_Time,''MM-W-YYYY'') AS Start_Date,
COUNT(Vlh.Thread#) Num_Logs
FROM V$log_History Vlh
WHERE Vlh.First_Time > current_date - interval ''30'' day
GROUP BY To_Char(Vlh.First_Time,''MM-W-YYYY'')) log_hist,
( select distinct bytes from V$log ) Vl
ORDER BY Log_Hist.Start_Date',',','EXT_TABLES','redo_history.log')
from dual;
Please find below dump_csv.sql:
CREATE OR REPLACE function dump_csv( p_query in varchar2,
p_separator in varchar2
default ',',
P_DIR in varchar2 ,
p_filename in varchar2 )
return number
AUTHID CURRENT_USER
is
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(2000);
l_status integer;
l_colCnt number default 0;
l_separator varchar2(10) default '';
l_cnt number default 0;
begin
l_output := utl_file.fopen( P_DIR, p_filename, 'w' );
dbms_sql.parse( l_theCursor, p_query, dbms_sql.native );
for i in 1 .. 255 loop
begin
dbms_sql.define_column( l_theCursor, i,
l_columnValue, 2000 );
l_colCnt := i;
exception
when others then
if ( sqlcode = -1007 ) then exit;
else
raise;
end if;
end;
end loop;
dbms_sql.define_column( l_theCursor, 1, l_columnValue,
2000 );
l_status := dbms_sql.execute(l_theCursor);
loop
exit when ( dbms_sql.fetch_rows(l_theCursor) <= 0 );
l_separator := '';
for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i,
l_columnValue );
utl_file.put( l_output, l_separator ||
l_columnValue );
l_separator := p_separator;
end loop;
utl_file.new_line( l_output );
l_cnt := l_cnt+1;
end loop;
dbms_sql.close_cursor(l_theCursor);
utl_file.fclose( l_output );
return l_cnt;
end dump_csv;
/
Thanks in advance for you HELP.
Regards,
Eddie