2020/08/02

String to Vector

範例 : 
DECLARE
  TYPE rVectorTabType IS TABLE OF VARCHAR2(40) INDEX BY BINARY_INTEGER;
  xVectorTab rVectorTabType;

  vDelimiter      VARCHAR2(1) := ':';
  vStringIn       VARCHAR2(32767) := 'A:B:C:D:E';
  vCounter        NUMBER;
  vDelimiterIndex NUMBER;
BEGIN
  vDelimiterIndex := INSTR(vStringIn, vDelimiter, 1);
  vCounter        := 0;
  WHILE vDelimiterIndex > 0
  LOOP
    xVectorTab(vCounter) := SUBSTR(vStringIn, 1, vDelimiterIndex - 1);
    vStringIn := SUBSTR(vStringIn, vDelimiterIndex + 1);
    vDelimiterIndex := INSTR(vStringIn, vDelimiter, 1);
    vCounter := vCounter + 1;
  END LOOP;
  xVectorTab(vCounter) := vStringIn;
  
  dbms_output.put_line('Table Count : ' || xVectorTab.count);
  FOR i IN xVectorTab.first .. xVectorTab.last LOOP 
    dbms_output.put_line('Element(' || i || ') =>' ||xVectorTab(i)); 
  END LOOP;
END;

結果 : 
Table Count : 5
Element(0) =>A
Element(1) =>B
Element(2) =>C
Element(3) =>D
Element(4) =>E

沒有留言: