![](/img/trans.png)
[英]How export a table data into a csv file using sqlplus in oracle 12.1
[英]export multiple table data into csv file in oracle
我有五個不同的表,如a,b,c,d,e,具有不同的列數。 我想將所有這五個表中的數據導出到csv文件中。 通常,我在所有五個表中都有seq_no。應該生成seq_no明智的文件。
和
table a data should be row 1
table b data should be row 2
table c data should be row 3
table d data should be row 4
table d data should be row n
table e data should be row n+1
在表a,b,c,e中,只有1條記錄用於1 seq_no
在表d中,多個記錄將存在1 seq_no。
例如
Seq_no = 1,則僅該數據應導出到csv。
seq_no = 2,那么只有該數據應該導出到csv
.....等
如果count(seq_no)= 10,則應導出10個文件。
我如何通過plsql函數/過程來實現這一目標?
創建由seq_no索引的文件句柄的關聯數組
type ta_file is table of utl_file.file_type index by number(5);
va_file ta_file;
迭代行並將其csv行放入文件中(您必須通過串聯替換'{csv string from x}'
)
for r in (
select * from (
select seq_no, '{csv string from a}' s, 'a' t from a
union all
select seq_no, '{csv string from b}' s, 'b' t from b
union all
select seq_no, '{csv string from c}' s, 'c' t from c
union all
select seq_no, '{csv string from d}' s, 'd' t from d
union all
select seq_no, '{csv string from e}' s, 'e' t from e
) order by t, seq_no
) loop
if not va_file.exists(r.seq_no) then
-- for new seq_no open new seq_no file
va_file(r.seq_no) := fopen(filepath, filename || r.seq_no || fileext, 'W');
end if;
-- write csv to seq_no file
utl_file.put_line(va_file(r.seq_no), r.s);
end loop;
關閉所有文件(通過va_file迭代)
for i in va_file.first .. va_file.last loop
utl_file.fflush(va_file(i));
utl_file.fclose(va_file(i));
end loop;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.