yamadaken1.hatenablog.com
上記の記事の関連です。
世の中にはストアドが嫌いな人もいるので。。。
JsonからテーブルにINSERTする話です。
テストデータは前回と同じです。
こんなテーブルを用意します。
CREATE TABLE test
(
item_code character varying
,product_name character varying
,req_qty numeric
)
;
テスト用なので、主キーもない手抜きのテーブルです。
このテーブルがあれば、次のSQLでJSON配列から一気にレコードINSERTが出来ます。
INSERT INTO test
SELECT
*
FROM
json_to_recordset
('
[
{
"item_code": "A0001",
"product_name": "KEYBORD",
"req_qty": 10.283
},
{
"item_code": "A0002",
"product_name": "MONITOR",
"req_qty": 526.267
},
{
"item_code": "A0003",
"product_name": "MOUSE",
"req_qty": 610.39
},
{
"item_code": "A0004",
"product_name": "MAIN BOX",
"req_qty": 341.751
},
{
"item_code": "A0005",
"product_name": "USB HUB",
"req_qty": 371.524
},
{
"item_code": "A0006",
"product_name": "ROOTER",
"req_qty": 1395
},
{
"item_code": "A0007",
"product_name": "WINDOWS10Pro",
"req_qty": 1287
},
{
"item_code": "A0008",
"product_name": "VISUAL STUDIO 2016 PRO",
"req_qty": 25
}
]
')
AS rec
(
item_code character varying
,product_name character varying
,req_qty numeric
)
;
SELECT結果はこんな感じになります。