将多个 table 数据导出到 oracle 中的 csv 文件
export multiple table data into csv file in oracle
我有五个不同的 tables 作为 a、b、c、d、e,具有不同的列数。
我想将这五个 table 的数据导出到 csv 文件中。
通常我在所有五个 tables.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
在 table a,b,c,e 中只有 1 条记录将存在 1 seq_no
在 table d 中将有 1 seq_no 的多条记录。
E.G
Seq_no = 1 那么只有该数据应该导出到 csv。
seq_no = 2 那么只有该数据应该导出到 csv
.....等等
if count(seq_no) = 10 那么应该导出 10 个文件。
如何通过 plsql function/procedure 实现此目的?
创建由 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;
我有五个不同的 tables 作为 a、b、c、d、e,具有不同的列数。 我想将这五个 table 的数据导出到 csv 文件中。 通常我在所有五个 tables.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
在 table a,b,c,e 中只有 1 条记录将存在 1 seq_no
在 table d 中将有 1 seq_no 的多条记录。
E.G
Seq_no = 1 那么只有该数据应该导出到 csv。
seq_no = 2 那么只有该数据应该导出到 csv
.....等等
if count(seq_no) = 10 那么应该导出 10 个文件。
如何通过 plsql function/procedure 实现此目的?
创建由 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;