次の例では、JSONインスタンスをソース データとして使用することでテーブルに入力します。例では、JSON文書のシュレッディングを行ないそこから値を抽出し、employeeテーブル(emp_table)にデータを挿入します。
この例を実行する前に、ソース テーブル(json_table)に対してSELECT権限があること、およびターゲット テーブル(emp_table)に対してINSERT権限があることを確認します。
CALL SYSLIB.JSON_SHRED_BATCH(
'SELECT id, empPersonalInfo, site
FROM test.json_table',
'[{"rowexpr" : "$.employees.info[*]",
"colexpr" : [{"col1" : "$.id",
"type" : "INTEGER"},
{"col2" : "$.employees.company",
"type" : "VARCHAR(15)",
"fromRoot" : true},
{"col3" : "$.name",
"type" : "VARCHAR(20)"},
{"col4" : "$.age",
"type" : "INTEGER"},
{"col5" : "$.dept",
"type" : "VARCHAR(10)"}],
"queryexpr" : [{"site" : "VARCHAR(20)"}],
"tables" : [
{"test.emp_table" : {
"metadata" : {
"operation" : "insert"
},
"columns" : {"empID" : "col1*100",
"company" : "col2",
"empName" : "col3",
"empAge" : "col4",
"dept" : "col5",
"startDate" : "CURRENT_DATE",
"site" : "site" }
}
}]
}]', :res );
シュレッドの結果としてemp_tableテーブルに3つの行が挿入され、ソース データとして使用するJSONオブジェクトの3つの項目に対応します。
結果を確認するには、次を実行します: SELECT empID, company, empName, empAge, startDate, site FROM emp_table ORDER BY empID;
empID company empName empAge startDate site ---------------------------------------------- 100 Teradata Cameron 24 13/09/19 RB 200 Teradata Justin 34 13/09/19 RB 300 Teradata Melissa 24 13/09/19 RB