[英]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.