Home » SQL & PL/SQL » SQL & PL/SQL » Oracle Function to create .CSV file
Oracle Function to create .CSV file [message #213368] Wed, 10 January 2007 08:09 Go to next message
JRDBA39
Messages: 3
Registered: June 2005
Junior Member
Gurus,

I'm using an oracle function to create a csv file. Everything works great except it adds a line at the end of the csv file. I've attached the function, please let me know if there is anything I can do so it doesn't add an extra line.

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;
Re: Oracle Function to create .CSV file [message #213369 is a reply to message #213368] Wed, 10 January 2007 08:12 Go to previous messageGo to next message
ebrian
Messages: 2794
Registered: April 2006
Senior Member
I believe that's the default action of UTL_FILE.
Re: Oracle Function to create .CSV file [message #213615 is a reply to message #213369] Thu, 11 January 2007 08:33 Go to previous message
Maaher
Messages: 7062
Registered: December 2001
Senior Member
The last thing you do in the loop is
 utl_file.new_line( l_output ); 


MHE
Previous Topic: Alternative Logic for Performance Enhancement!!!
Next Topic: select all but one column from a large table
Goto Forum:
  


Current Time: Thu Dec 08 23:53:42 CST 2016

Total time taken to generate the page: 0.22250 seconds