我正在尝试展平由FOR JSON
.
我的查询看起来像:
select
(
select id from MyTable
where id in (select value from OPENJSON(@jsonArray))
FOR JSON PATH
) existing,
(
select value id from OPENJSON(@jsonArray)
where value not in (select Id from MyTable)
FOR JSON PATH
) missing
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
生成的 JSON 是:
{
"existing": [
{
"id": "a00cd8f6-d1c6-4604-b235-59d3cacd5bcc"
},
{
"id": "052455b6-6bf5-47d3-8bee-7ba98d7fbd50"
}
],
"missing": [
{
"id": "328add2d-e8f2-4a0e-af54-5b1733310170"
}
]
}
我想要的是:
{
"existing": [
{
"id": "a00cd8f6-d1c6-4604-b235-59d3cacd5bcc"
},
{
"id": "052455b6-6bf5-47d3-8bee-7ba98d7fbd50"
}
],
"missing": [
"328add2d-e8f2-4a0e-af54-5b1733310170"
]
}
缺少的数组不应包含 json 对象,而应仅包含值。有什么建议么?