日期:2014-05-16 浏览次数:20462 次
?
?
CREATE OR REPLACE procedure PULSEINP.dump_csv( p_separator in varchar2 default ',' ) is l_conn UTL_TCP.connection; 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 ','; p_filename varchar2(50) ; l_cnt number default 0; p_query varchar2(5000); begin p_filename := 'CMN_TDRDAILY'||to_char(sysdate-(1/24), 'HH24')||'.CSV'; l_output := utl_file.fopen( 'UTL_FILE_DIR', p_filename, 'w' ); p_query :='select REQUEST_TYPE,CALLSTARTTIME,ANI,DNIS,ROUTE1LABEL,ANI,LRN,DIALCODE,ANI FROM CMN_TDRDAILY'||to_char(sysdate-(1/24), 'HH24'); DBMS_OUTPUT.PUT_LINE(p_query); 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_OUTPUT.PUT_LINE(l_colCnt); dbms_sql.define_column( l_theCursor, 1, l_columnValue, 2000 ); l_status := dbms_sql.execute(l_theCursor); utl_file.put( l_output, 'CALLTYPE,STARTTIME,ORIGINATINGNUMBER,TERMINATINGNUMBER,TERMINATINGTRUNKNUMBER,col25,col26,col27,col28,col50,CALLCONNECTTIME,CALLANSWERTIME,CALLDISCONNECTTIME'); utl_file.new_line( l_output ); 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.put( l_output, ',,'||TO_CHAR(SYSDATE-5/(24*60), 'dd-mm-yy hh24:mi:ss')||','||TO_CHAR(SYSDATE-1/(24*60), 'dd-mm-yy hh24:mi:ss')); utl_file.new_line( l_output ); l_cnt := l_cnt+1; end loop; dbms_sql.close_cursor(l_theCursor); utl_file.fclose( l_output ); l_conn := ftp.login('10.0.1.29', '21', 'oracle', 'oracle'); ftp.ascii(p_conn => l_conn); ftp.put(p_conn => l_conn, p_from_dir => 'MY_DOCS', p_from_file => p_filename, p_to_file => '/home/oracle/'||p_filename); ftp.logout(l_conn); utl_tcp.close_all_connections; end dump_csv; /