有没有办法使用utl_file从oracle plsql中的32000以上的数据生成csv

时间:2015-08-12 13:16:21

标签: oracle csv plsql utl-file

.. FILE1:= UTL_FILE.FOPEN('DIR','cg.csv','w',32000); .. 我们正在生成一个csv文件,该文件具有视图的多列的连接值。 在oracle plsql中有没有办法使用utl_file从超过32000的数据生成csv

3 个答案:

答案 0 :(得分:0)

根据Oracle's documentation功能UTL_FILE.FOPEN

  

FOPEN max_linesize参数必须是1和32767范围内的数字

所以你可以把它增加到32767,但不会超过那个。

您可能需要考虑创建多个文件。

答案 1 :(得分:0)

declare
FILEHANDLE           UTL_FILE.FILE_TYPE;
WRITEMESSAGE   varchar2(200);
longLine varchar2(32767);
newline char(2) :=  CHR(13) || CHR(10);
begin
longLine :=  LPAD('aaaa', 32766,'x');
FILEHANDLE        := UTL_FILE.FOPEN('XMLDIR','lonLineFile.txt','wb',32767);
for i in 1 .. 5 loop
  UTL_FILE.PUT_RAW (filehandle,UTL_RAW.CAST_TO_RAW (longLine),true); 
  UTL_FILE.PUT_RAW (filehandle,UTL_RAW.CAST_TO_RAW (longLine),true); 
  UTL_FILE.PUT_RAW (filehandle,UTL_RAW.CAST_TO_RAW (longLine),true); 
  UTL_FILE.PUT_RAW (filehandle,UTL_RAW.CAST_TO_RAW (newline),true); 
end loop;
  UTL_FILE.FCLOSE(filehandle);
end;
/

在' wb'中打开文件写字节模式。接下来将raw写入文件和行尾char。

答案 2 :(得分:0)

是的,有一种方法 - 使用clob数据类型而不是varchar2clob maximum size in PL/SQL是128TB。

在PL / SQL代码中,首先将数据收集到temporary clob。第二次以块的形式迭代clob并将其提供给utl_file

下面是一个随机的互联网代码段,它将任意clob写入用户定义的文件:

procedure save (
  p_text in clob,
  p_path in varchar2,
  p_filename in varchar2
) as
  v_lob_temp blob;
begin
  --
  -- exit if any parameter is null
  --
  if    p_text is null
     or p_path is null
     or p_filename is null then
    return;
  end if;

  --
  -- convert a clob to a blob
  --
  declare
    v_dest_offset pls_integer := 1;
    v_src_offset pls_integer := 1;
    v_lang_context pls_integer := dbms_lob.default_lang_ctx;
    v_warning pls_integer := dbms_lob.no_warning;
  begin
    dbms_lob.createtemporary(lob_loc => v_lob_temp,
                             cache => true,
                             dur => dbms_lob.call);

    dbms_lob.converttoblob(dest_lob => v_lob_temp,
                           src_clob => p_text,
                           amount => dbms_lob.lobmaxsize,
                           dest_offset => v_dest_offset,
                           src_offset => v_src_offset,
                           blob_csid => dbms_lob.default_csid,
                           lang_context => v_lang_context,
                           warning => v_warning);
    -- TODO raise (what?) when warning
  end;

  --
  -- write a blob to a file
  --
  declare
    v_lob_len pls_integer;
    v_fh utl_file.file_type;
    v_pos pls_integer := 1;
    v_buffer raw(32767);
    v_amount pls_integer := 32767;
  begin
    v_fh := utl_file.fopen(p_path, p_filename, 'wb', 32767);

    v_lob_len := dbms_lob.getlength(v_lob_temp);

    while v_pos < v_lob_len loop
      dbms_lob.read(v_lob_temp, v_amount, v_pos, v_buffer);

      utl_file.put_raw(file => v_fh,
                       buffer =>v_buffer,
                       autoflush => false);

      v_pos := v_pos + v_amount;
    end loop;

    utl_file.fclose(v_fh);
    dbms_lob.freetemporary(v_lob_temp);
  end;

end;