Splitting comma separated string in a PL/SQL stored proc

前端 未结 8 1799
别跟我提以往
别跟我提以往 2020-11-30 05:08

I\'ve CSV string 100.01,200.02,300.03 which I need to pass to a PL/SQL stored procedure in Oracle. Inside the proc,I need to insert these values in a Number column in the ta

8条回答
  •  粉色の甜心
    2020-11-30 05:42

    I am not sure if this fits your oracle version. On my 10g I can use pipelined table functions:

    set serveroutput on
    
    create type number_list as table of number;
    
    -- since you want this solution
    create or replace function split_csv (i_csv varchar2) return number_list pipelined 
      is 
        mystring varchar2(2000):= i_csv;
      begin
        for r in
        ( select regexp_substr(mystring,'[^,]+',1,level) element
            from dual
         connect by level <= length(regexp_replace(mystring,'[^,]+')) + 1
        )
        loop
          --dbms_output.put_line(r.element);
          pipe row(to_number(r.element, '999999.99'));
        end loop;
      end;
    /
    
    insert into foo
    select column_a,column_b from 
      (select column_value column_a, rownum rn from table(split_csv('0.75, 0.64, 0.56, 0.45'))) a 
     ,(select column_value column_b, rownum rn from table(split_csv('0.25, 0.5, 0.65, 0.8'))) b
     where a.rn = b.rn
    ;
    

提交回复
热议问题