Вам нужно openjson
на нескольких уровнях. Как то так.
declare @json nvarchar(max)=N'{
"items": [
{ "ids": [4], "fromCompanyId": 4 },
{ "ids": [6, 1], "fromCompanyId": 1 }
]
}'
select id,fromCompanyId
from openjson(@json,'$.items') j --path to the main array
cross apply openjson(value,'$.ids') -- path inside element of main array
with(id int '$')
cross apply openjson(value)
with (
fromCompanyId int '$.fromCompanyId'
)
where fromCompanyId=4
Аналогично полю таблицы.
declare @tbl table (id int, detail nvarchar(max))
insert @tbl (id,detail) values
(1,N'{
"items": [
{ "ids": [4], "fromCompanyId": 4 },
{ "ids": [6, 1], "fromCompanyId": 1 }
]
}'),
(2,N'{
"items": [
{ "ids": [5], "fromCompanyId": 4 },
{ "ids": [7,9], "fromCompanyId": 4 },
{ "ids": [6, 1], "fromCompanyId": 1 }
]
}')
select id,jid,fromCompanyId
from @tbl
cross apply openjson(detail,'$.items') -- path to the main array
cross apply openjson(value,'$.ids') -- path inside array element
with(jid int '$')
cross apply openjson(value)
with (
fromCompanyId int '$.fromCompanyId'
)
where fromCompanyId=4