如何将(批量)Handsontable Json数组插入到oracle表中?

时间:2017-05-02 13:36:23

标签: javascript json oracle plsql handsontable

var hot = new Handsontable(container, {}
JSON.stringify({data: hot.getData()})

JSON: { data:  [
      "col_1" // These are the table columns 
      "col_2"     
    ],
    [
      "test2",// there are values
      "201702"      
    ],
    [
      "test2",// there are values
      "201702"
    ]
}

这里的列总是动态的。我需要相应地构建插入查询。有些人可以帮助我如何将handontable数据插入表中。

3 个答案:

答案 0 :(得分:1)

这在12.1.0.2.0中对我有用

SELECT * 
FROM   JSON_TABLE (' [["USD" , "GBP", 1.2],
                  ["USD" , "EUR", 1.1],
                  ["GBP" , "EUR",  .9]]', '$[*]'  columns ( 
                                          c varchar2(100) path '$[0]',
                                          tc varchar2(100) path '$[1]',
                                          cr number path '$[2]'));

答案 1 :(得分:0)

你JSON对初学者无效..

但是如果我假设你想拥有一个数组数组,那么像这样的东西应该适合你

SQL> create table SOURCE_DOCUMENTS (
  2    ID NUMBER,
  3    JSON_DOC CLOB CHECK (JSON_DOC IS JSON)
  4  )
  5  /

Table created.

SQL> insert into SOURCE_DOCUMENTS values (1,'{
  2  "data": [
  3  [
  4  "col_1", "col_2"
  5  ], [
  6  "test2", "201702"
  7  ], [
  8  "test2", "201702"
  9  ]
 10  ]
 11  }')
 12  /

1 row created.

SQL> commit
  2  /

Commit complete.

SQL> create or replace procedure insertFromJSON(P_ID NUMBER)
  2  AUTHID CURRENT_USER
  3  IS
  4    JSON_DOC         CLOB;
  5    JO               JSON_OBJECT_T;
  6    DATA             JSON_ARRAY_T;
  7    COLUMN_DETAILS   JSON_ARRAY_T;
  8    I                PLS_INTEGER := 0;
  9    COLUMN_LIST      VARCHAR2(32767) := '';
 10    COLUMN_PATTERN   VARCHAR2(32767) := '';
 11    STATEMENT        VARCHAR2(32767) := '';
 12  begin
 13
 14    SELECT JSON_DOC
 15      into JSON_DOC
 16      from SOURCE_DOCUMENTS
 17     where ID = P_ID;
 18
 19    JO   := JSON_OBJECT_T(JSON_DOC);
 20    DATA := TREAT(JO.get('data') as JSON_ARRAY_T);
 21    COLUMN_DETAILS := TREAT(DATA.get(0) as JSON_ARRAY_T);
 22    DBMS_OUTPUT.put_line('Column Count = ' || COLUMN_DETAILS.get_size());
 23    WHILE (i < COLUMN_DETAILS.get_size()) LOOP
 24      COLUMN_LIST := COLUMN_LIST || ',' || '"' || COLUMN_DETAILS.get_string(i) || '"';
 25      COLUMN_PATTERN := COLUMN_PATTERN || ',' || '"' || COLUMN_DETAILS.get_string(i) || '"' || ' VARCHAR2(32) PATH ''
$[' || i || ']''';
 26      i := i + 1;
 27    end loop;
 28    COLUMN_LIST := SUBSTR(COLUMN_LIST,2);
 29    COLUMN_PATTERN := SUBSTR(COLUMN_PATTERN,2);
 30    -- COLUMN_PATTERN := 'IDX FOR ORDINALITY ' || COLUMN_PATTERN;
 31    STATEMENT := 'CREATE TABLE TEST as select ' || COLUMN_LIST || ' from ( select ROWNUM ROW_NUM, ' || COLUMN_LIST ||
 ' FROM SOURCE_DOCUMENTS, JSON_TABLE(JSON_DOC,''$.data[*]'' columns ' || COLUMN_PATTERN || ') where ID = ' || P_ID || ')
 WHERE ROW_NUM > 1';
 32    DBMS_OUTPUT.put_line('Statement = ' || STATEMENT);
 33    EXECUTE IMMEDIATE STATEMENT;
 34  end;
 35  /

Procedure created.

SQL> show errors
No errors.
SQL> --
SQL> set serveroutput on
SQL> /

Procedure created.

SQL> call insertFromJSON(1)
  2  /
Column Count = 2
Statement = CREATE TABLE TEST as select "col_1","col_2" from ( select ROWNUM
ROW_NUM, "col_1","col_2" FROM SOURCE_DOCUMENTS, JSON_TABLE(JSON_DOC,'$.data[*]'
columns "col_1" VARCHAR2(32) PATH '$[0]',"col_2" VARCHAR2(32) PATH '$[1]') where
ID = 1) WHERE ROW_NUM > 1

Call completed.

SQL> commit
  2  /

Commit complete.

SQL> select *
  2    from TEST
  3  /

col_1                            col_2
-------------------------------- --------------------------------
test2                            201702
test2                            201702

SQL>

答案 2 :(得分:0)

这是一个应该适用于12.1.0.2.0

的替代方案
SQL> create or replace procedure insertFromJSON(P_ID NUMBER)
   2   AUTHID CURRENT_USER
   3   IS
   8     I                PLS_INTEGER := 0;
   9     COLUMN_LIST      VARCHAR2(32767) := '';
  10     COLUMN_PATTERN   VARCHAR2(32767) := '';
  11     STATEMENT        VARCHAR2(32767) := '';
  12
  13     CURSOR COL_CURSOR
  14     is
  15     select COLUMN_NAME
  16       from SOURCE_DOCUMENTS,
  17            JSON_TABLE(
  18              JSON_DOC,
  19              '$.data[0][*]'
  20              columns
  21                COLUMN_NAME VARCHAR2(32) path '$'
  22            );
  23
  24   begin
  25
  26    for C in COL_CURSOR LOOP
  27      COLUMN_LIST := COLUMN_LIST || ',' || '"' || c.COLUMN_NAME || '"';
  28      COLUMN_PATTERN := COLUMN_PATTERN || ',' || '"' || c.COLUMN_NAME || '"' || ' VARCHAR2(32) PATH '' $[' || i || '
 ''';
  29      i := i + 1;
  30    end loop;
  31
  32    COLUMN_LIST := SUBSTR(COLUMN_LIST,2);
  33    COLUMN_PATTERN := SUBSTR(COLUMN_PATTERN,2);
  34    STATEMENT := 'CREATE TABLE TEST as select ' || COLUMN_LIST || ' from ( select ROWNUM ROW_NUM, ' || COLUMN_LIST |
  ' FROM SOURCE_DOCUMENTS, JSON_TABLE(JSON_DOC,''$.data[*]'' columns ' || COLUMN_PATTERN || ') where ID = ' || P_ID || '
  WHERE ROW_NUM > 1';
  35    DBMS_OUTPUT.put_line('Statement = ' || STATEMENT);
  36    EXECUTE IMMEDIATE STATEMENT;
  37   end;
  38  /

 Procedure created.

 SQL> drop table TEST
   2  /

 Table dropped.

 SQL> call insertFromJSON(1)
   2  /

 Call completed.

 SQL> commit
   2  /

 Commit complete.

 SQL> select *
   2    from TEST
   3  /

 col_1                            col_2
 -------------------------------- --------------------------------
 test2                            201702
 test2                            201702