-2

好的,所以,这很难解释,但我会试一试。谷歌没有帮助我,所以如果它可以帮助其他人,请更新这个问题。

背景

我有一个表格Persons,其中有一些列,例如 [ID]、[Name] 和 [PhoneNumbers]。该表正在填充来自第三方系统的数据,因此我无法更改我们插入数据的方式。

[PhoneNumbers] 列包含一个 JSON 数字数组,如下所示:

{"phonenumbers":[]}

我现在正在尝试针对该表编写一个视图,目标是每个数字都有一行。

问题

我可以使用 T-SQL 和它的 JSON 支持来实现这一点吗?我正在使用 SQL Server 2016。

4

1 回答 1

2
declare @j nvarchar(max) = N'{"phonenumbers":["1a", "2b", "3c", "4", "5", "6", "7", "8", "9", "10x"]}';

select value, *
from openjson(@j, '$.phonenumbers');

declare @t table
(
id int identity,
phonenumbers nvarchar(max)
);

insert into @t(phonenumbers)
values(N'{"phonenumbers":["1a", "2b", "3c", "4d"]}'), (N'{"phonenumbers":["22", "23", "24", "25"]}'), (N'{"phonenumbers":[]}'), (NULL);

select id, j.value, j.[key]+1 as phone_no_ordinal, t.*
from @t as t
outer apply openjson(t.phonenumbers, '$.phonenumbers') as j;
于 2020-03-17T15:33:03.123 回答