procedure prc_write_file (p_query in varchar2, --sql query statement
p_table_name in varchar2, --table name
p_max_linesize in number default 32000 )is --max linesize,must less than 32787
l_output utl_file.file_type;
l_thecursor integer default dbms_sql.open_cursor;
l_columnvalue varchar2(4000);
l_status integer;
l_colcnt number := 0;
l_separator varchar2(1);
l_desctbl dbms_sql.desc_tab;
begin
--open file
l_output := utl_file.fopen( 'h**_buckup_csv_dir',p_table_name || '_' || substr(to_char(sysdate, 'yyyymmdd'),0,6) || '.csv', 'w', p_max_linesize);
--define date format
execute immediate 'alter session set nls_date_format=''yyyy/mm/dd hh24:mi:ss''';
--open cursor
dbms_sql.parse(l_thecursor, p_query, dbms_sql.native );
dbms_sql.describe_columns(l_thecursor, l_colcnt, l_desctbl );
--dump table column name
for i in 1 .. l_colcnt loop
dbms_sql.define_column(l_thecursor, i, l_columnvalue, 4000 );
l_separator := ',';
end loop;
--execute the query statement
l_status := dbms_sql.execute(l_thecursor);
--dump table column value
while ( dbms_sql.fetch_rows(l_thecursor) > 0 ) loop
l_separator := '';
for i in 1 .. l_colcnt loop
dbms_sql.column_value(l_thecursor, i, l_columnvalue );
--shuzhi
if fnc_isnumber(l_columnvalue) = 1 then
utl_file.put(l_output, l_separator ||
trim(leading '9' from trim(trim(trailing '.' from
trim(trailing '0' from to_char(l_columnvalue,'9999999990.00000'))))));
else
utl_file.put(l_output, l_separator || l_columnvalue);
end if;
l_separator := ',';
end loop;
utl_file.new_line(l_output);
end loop;
--close cursor
dbms_sql.close_cursor(l_thecursor);
--close file
utl_file.fclose(l_output);
exception
when others then
raise;
end;
還要判斷欄位的值是否是數值型別的!
function fnc_isnumber(pvalue in varchar2 ) return number is
lvalue varchar2(200);
begin
lvalue := to_number(pvalue);
if instrb(pvalue,'.') = 0 then
return 2;
else
return 1;
end if;
exception
when others then
return 0;
end;
VBA將Excel匯出為CSV檔案
sub csv dim fs,myfile as object dim myfileline as string txtfile的行資料 dim sht as worksheet dim csvfilename as string csv檔名 dim totalrows as integer 總的行...
ORACLE匯出CSV檔案
oracle的匯出功能會經常使用,也同樣經常會有需求進行匯出為csv檔案,用於往別的資料庫進行遷移,本次例項及在oracle實現匯出csv檔案操作。sqlplus as sysdba sql create or replace directory out path as home wenxuecha...
java web匯出csv檔案
用csv匯出取代excel匯出的原因 poi匯出excel時會出現以下問題 excel2003 單sheet最多只能匯出65536條資料 excel2007 單sheet能匯出百萬級資料,彌補了2003的缺陷 excel每建立乙個單元格就會生成乙個物件,並且excel生成過程中不會釋放物件,所以會占...