如何循环遍历 Oracle PLSQL 中的分隔列表

2021-12-24 00:00:00 csv loops oracle plsql

我正在研究一个 Oracle 过程,该过程在其中调用另一个过程.我的参数之一 (parm1) 可以在逗号分隔列表中包含一个或多个值.如何遍历这些值以将它们一次传递给另一个过程?

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 list. How can I loop through these values to pass them one at a time to another procedure?

这是我希望它做的一个例子:

Here is an example of what I would like it to do:

When Parm1 = 123,312

callProcedure2(123)
callProcedure2(321)

-或-

When Parm1 123

callProcedure2(123)

我认为这可以使用循环来完成,但我不知道如何让它将每个值用作循环内的单独调用.

I think this can be accomplished using a loop but I can't figure out how to get it to use each value as a separated call within the loop.

任何帮助将不胜感激!

谢谢!

推荐答案

只需循环子串:

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;

相关文章