将JSON数组批量插入到SQL Server表列中

时间:2018-12-04 06:11:52

标签: sql sql-server sql-server-2016

如何在不使用JSON文件的情况下将JSON数组批量插入具有分隔列的SQL Server 2016表中(将从输入中获取带有外部对象的值)

可以找到示例JSON here

1 个答案:

答案 0 :(得分:0)

您可以尝试[未测试]:

[
 { "code" : 001,"name": "Prashant" },
 { "code" : 002,"name": "Steve" }
]

查询:

INSERT INTO your_table (code, name)
SELECT code,name
FROM OPENJSON(@json)
     WITH (code int, name nvarchar(50))

要从JSON对象读取:

{"OUTER":{"ABC":"TEST_WB_New","XYZ":"9085"}} -- Your JSON

INSERT INTO your_table_name
SELECT ABC, XYZ
FROM OPENJSON(@json)
 WITH (
       ABC nvarchar(50) 'strict $.OUTER.ABC',
       XYZ nvarchar(50) '$.OUTER.XYZ' AS JSON
)