Split multi column semicolon separated string and create records

前端 未结 2 643
心在旅途
心在旅途 2021-01-23 22:25

I have a stored procedure which receives three string parameters

1. p_ReqIDs VARCHAR2
2. p_ItemIDs VARCHAR2
3. p_Qtys    VARCHAR2

The above 3

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-23 22:52

    You could process parameters as follows:

    SQL> declare
      2    type l_rec_type is record(
      3      r_ReqIDs  varchar2(31),
      4      r_ItemIDs varchar2(31),
      5      r_Qtys    varchar2(31)
      6    );
      7  
      8    type l_rec_list is table of l_rec_type;
      9    -- your parameters. 
     10    l_ReqIDs  constant varchar2(31) := '56;56;56;';
     11    l_ItemIDs constant varchar2(31) := '3;2;1;';
     12    l_Qtys    constant varchar2(31) := '400;300;200;';
     13  
     14    l_rec l_rec_list;
     15  begin
     16  
     17  with Parameters(param) as(
     18      select l_ReqIDs  from dual union all
     19      select l_ItemIDs from dual union all
     20      select l_Qtys    from dual
     21    ),
     22    Occurrences(oc) as(
     23      select level
     24        from ( select max(regexp_count(param, '[^;]+')) moc
     25                 from parameters) s
     26     connect by level <= s.moc
     27    )
     28  select max(res1)
     29       , max(res2)
     30       , max(res3)
     31    bulk collect into l_rec
     32    from (select decode(param, l_ReqIDs, res) res1
     33               , decode(param, l_ItemIDs,res) res2
     34               , decode(param, l_Qtys,   res) res3
     35               , rn
     36            from ( select param, regexp_substr(param, '[^;]+', 1, o.oc) res
     37                        , row_number() over(partition by param order by param) rn
     38                     from parameters p
     39                    cross join occurrences o
     40                  )
     41          )
     42  group by rn;
     43  
     44    for i in l_rec.first..l_rec.last
     45    loop
     46       dbms_output.put_line(l_rec(i).r_ReqIDs || '  ' || l_rec(i).r_ItemIDs || '  ' || l_rec(i).r_Qtys);
     47    end loop;
     48  end;
     49  /
    
    
    
    56   2   200
    56   1   300
    56   3   400
    
    PL/SQL procedure successfully completed
    

    If you need to just insert processed data into a table the only insert into statement and query will be needed (bulk collect into l_rec must be removed):

    insert into your_table(<>)
      with Parameters(param) as(
         select l_ReqIDs  from dual union all
         select l_ItemIDs from dual union all
         select l_Qtys    from dual
      .... 
      -- the rest of the query from the above pl/sql block.
    

    You also can insert an entire record into a table(in a case you need to do extra processing of extracted elements before insertion) as follows:

    • If number of columns in a table equal to number of elements being inserted

      for i in l_rec.first..l_rec.last
      loop
         insert into your_table
           values l_rec(i);
      end loop;
      
    • If number of columns in a table is greater then the number of values being inserted

      for i in l_rec.first..l_rec.last
      loop
         insert into (select column1, .. ,columnn from your_table)
           values l_rec(i);
      end loop;
      

提交回复
热议问题