欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

lob to csv

程序员文章站 2023-12-30 16:12:58
...

将相应的 大型对象LOB类型数据转为相应的CSV格式 返回 ,采用了piplined 的方式,也就是 运行出一条数据 就返回一条 ,不是全部都执行完了 再返回的形式 PL/SQL lob-to-csv piplined function clob_to_csv (p_csv_clob in clob, p_separator in varchar2 := g

将相应的 大型对象LOB类型数据转为相应的CSV格式 返回 ,采用了piplined 的方式,也就是 运行出一条数据 就返回一条 ,不是全部都执行完了 再返回的形式 PL/SQL lob-to-csv piplined
function clob_to_csv (p_csv_clob in clob,
                      p_separator in varchar2 := g_default_separator,
                      p_skip_rows in number := 0) return t_csv_tab pipelined
as
  l_line_separator         varchar2(2) := chr(13) || chr(10);--行的 分割符号 \r\n 
  l_last                   pls_integer;--上一次的扫描位置
  l_current                pls_integer;--这一次的扫描位置
  l_line                   varchar2(32000);
  l_line_number            pls_integer := 0;
  l_from_line              pls_integer := p_skip_rows + 1;
  l_line_array             t_str_array;
  l_row                    t_csv_line := t_csv_line (null, null,  -- line number, line raw
                                                     null, null, null, null, null, null, null, null, null, null,   -- lines 1-10
                                                     null, null, null, null, null, null, null, null, null, null);  -- lines 11-20
begin

  /*

  Purpose:      convert clob to CSV

  Remarks:      based on code from http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1352202934074
                              and  http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:744825627183

  Who     Date        Description
  ------  ----------  --------------------------------
  MBR     31.03.2010  Created
  fartpig 07.03.2011  noted
  */

  -- If the file has a DOS newline (cr+lf), use that: 如果文件时DOS的格式就是用 \r\n
  -- If the file does not have a DOS newline, use a Unix newline (lf) 如果不是就采用 unix标准 \n
  -- 通过检索 \r\n 是否存在 
  if (nvl(dbms_lob.instr(p_csv_clob, l_line_separator, 1, 1),0) = 0) then
    l_line_separator := chr(10);
  end if;

  l_last := 1;--设定上一次扫描位置为 1 

  loop
    --检索 当前分割符号的位置
    --为了能够顺利的将文件读完 需要 将传入的 LOB结尾加上一个 分割符号
    l_current := dbms_lob.instr (p_csv_clob || l_line_separator, l_line_separator, l_last, 1);
    --当 没有找到时候 退出
    exit when (nvl(l_current,0) = 0);
    --递增  行号
    l_line_number := l_line_number + 1;
    
    if l_from_line 

上一篇:

下一篇: