Oracle-Insert逗号分隔的字符串值到表中 -

时间:2015-10-27 11:00:13

标签: oracle

尝试将过程输入参数接受的整行插入表格中以逗号分隔的字符串'THIS,IS,AN,Example,null,STRING'

以下是我的尝试:

DECLARE
  I_NPUT VARCHAR2(4000) := 'THIS,IS,AN,Example,null,STRING# This , is ,an,another,ex,null#'The,list,goes,on,on,null'';
  L_COUNT BINARY_INTEGER;
  L_ARRAY DBMS_UTILITY.LNAME_ARRAY;
BEGIN
  DBMS_UTILITY.COMMA_TO_TABLE(LIST => REGEXP_REPLACE(I_NPUT, '(^|,)', '\1x'), TABLEN => L_COUNT, TAB => L_ARRAY);
  DBMS_OUTPUT.PUT_LINE(L_COUNT);
  FOR I IN 1 .. L_COUNT
  LOOP
    DBMS_OUTPUT.PUT_LINE('Element ' || TO_CHAR(I) || ' of array contains: ' || SUBSTR(L_ARRAY(I), 2));
    INSERT INTO TEST VALUES
      (SUBSTR(L_ARRAY(I), 2)
      );
    COMMIT;
  END LOOP;
END; 

回报必须是:

COL_1    COL_2   COL_3  COL_4   COL_5  COL_6
-----------------------------------------------
THIS     IS      AN     Example null   STRING

1 个答案:

答案 0 :(得分:1)

sss := replace(p_string,',',''',''');
sss := regexp_replace(sss, '''null''','null');

EXECUTE IMMEDIATE 'INSERT INTO your_table (COL_1, COL_2, COL_3, COL_4, COL_5, COL_6) 
VALUES (''' ||sss|| ''')';

这里是p_string - 你的输入字符串