简体   繁体   English

将多个表数据导出到oracle中的csv文件中

[英]export multiple table data into csv file in oracle

I have five different tables as a,b,c,d,e with different number of columns. 我有五个不同的表,如a,b,c,d,e,具有不同的列数。 I want to export the data into the csv file from all this five table. 我想将所有这五个表中的数据导出到csv文件中。 In common i have seq_no in all five tables.seq_no wise file should get generated. 通常,我在所有五个表中都有seq_no。应该生成seq_no明智的文件。

And

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

In table a,b,c,e only 1 record will be there for 1 seq_no 在表a,b,c,e中,只有1条记录用于1 seq_no

In table d multiple records will be there for 1 seq_no. 在表d中,多个记录将存在1 seq_no。

EG 例如

Seq_no = 1 then only that data should get exported to the csv. Seq_no = 1,则仅该数据应导出到csv。

seq_no = 2 then only that data should get exported to the csv seq_no = 2,那么只有该数据应该导出到csv

..... etc .....等

if count(seq_no) = 10 then 10 files should get exported. 如果count(seq_no)= 10,则应导出10个文件。

how can i achive this through plsql function/procedure? 我如何通过plsql函数/过程来实现这一目标?

create associative array of file handles indexed by seq_no 创建由seq_no索引的文件句柄的关联数组

type ta_file is table of utl_file.file_type index by number(5);
va_file ta_file;

iterate rows and put their csv lines into files (you have to replace '{csv string from x}' by concatenation) 迭代行并将其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;

close all files (iterating through va_file) 关闭所有文件(通过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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM