How to loop through a delimited list in Oracle PLSQL

后端 未结 4 1173
青春惊慌失措
青春惊慌失措 2021-01-01 05:40

I am working on an Oracle procedure that calls another procedure within it. One of my parameters (parm1) can contain one or more values in a comma separated lis

4条回答
  •  隐瞒了意图╮
    2021-01-01 06:25

    Just loop through substrings:

    declare 
      parm1 varchar2(1000) := '123,234,345,456,567,789,890';
    
      vStartIdx binary_integer;
      vEndIdx   binary_integer;
      vCurValue varchar2(1000);
    begin
    
      vStartIdx := 0;
      vEndIdx   := instr(parm1, ','); 
    
      while(vEndIdx > 0) loop
        vCurValue := substr(parm1, vStartIdx+1, vEndIdx - vStartIdx - 1);
    
        -- call proc here
        dbms_output.put_line('->'||vCurValue||'<-');
    
        vStartIdx := vEndIdx;
        vEndIdx := instr(parm1, ',', vStartIdx + 1);  
      end loop;
    
      -- Call proc here for last part (or in case of single element)
      vCurValue := substr(parm1, vStartIdx+1);
      dbms_output.put_line('->'||vCurValue||'<-');
    
    end;
    

提交回复
热议问题