將table內容輸出為csv檔案

2022-08-22 14:42:09 字數 2613 閱讀 3082

1、建立 directory

create or

replace

directory

tt as

'c:\'

;select *

from

dba_objects do

where

do.object_type

like

'%dir%'; --- 查詢目錄是否建立成功

2、輸出指令碼:來自asktom**

create or replace procedure dump_table_to_csv(p_tname    in

varchar2,

p_dir

invarchar2,

p_filename

in varchar2) authid current_user /*

<<<== if you want... runs as "invoker" and runs with roles

*/is

l_output utl_file.file_type;

l_thecursor integer

default

dbms_sql.open_cursor;

l_columnvalue varchar2(

4000

); l_status integer;

l_query varchar2(

1000) default

'select * from ' ||p_tname;

l_colcnt number := 0

; l_separator varchar2(1);

l_desctbl dbms_sql.desc_tab;

begin

l_output := utl_file.fopen(p_dir, p_filename, 'w'

); execute immediate

'alter session set nls_date_format=

''dd-mon-yyyy hh24:mi:ss'''

; dbms_sql.parse(l_thecursor, l_query, dbms_sql.native);

dbms_sql.describe_columns(l_thecursor, l_colcnt, l_desctbl);

for i in

1.. l_colcnt loop

utl_file.put(l_output,

l_separator || '

"' || l_desctbl(i).col_name || '"'

); dbms_sql.define_column(l_thecursor, i, l_columnvalue,

4000

); l_separator := ','

; end loop;

utl_file.new_line(l_output);

l_status :=dbms_sql.execute(l_thecursor);

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);

utl_file.put(l_output, l_separator ||l_columnvalue);

l_separator := ','

; end loop;

utl_file.new_line(l_output);

end loop;

dbms_sql.close_cursor(l_thecursor);

utl_file.fclose(l_output);

execute immediate

'alter session set nls_date_format=

''dd-mon-yy'''

;exception

when others then

execute immediate

'alter session set nls_date_format=

''dd-mon-yy'''

; raise;

end;

3、執行

sql> exec dump_table_to_csv(p_tname =>'test',p_dir =>'tt',p_filename => 'tt.csv' );

pl/sql procedure successfully completed

由於utl_file.fopen(p_dir, p_filename, 'w') 儲存過程要求p_dir必須為大寫字元,所以這裡要注意,如果不是全大寫,會出現

ora-29280: invalid directory path 錯誤

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 總的行...

Python將list元素轉存為CSV檔案

首先先定義乙個list,將其轉存為csv檔案,看將會報什麼錯誤 list 1,2,3 4,5,6 7,9,9 list.to csv e testcsv.csv encoding utf 8 執行後出現 traceback most recent call last file d python un...

js將element的table表匯出為excel

直接上 匯出全部 只要左邊的 就行了 settimeout function var html str 例項化乙個blob物件,其建構函式的第乙個引數是包含檔案內容的陣列,第二個引數是包含檔案型別屬性的物件 var a document.getelementbyid downland 利用url.c...