1

我有一张桌子:员工。架构:(名称 varchar,首选项 nvarchar(max))。其中,preferences 是一个 json 格式的字符串。

我的查询(失败)是:

select JSON_QUERY(preferences, '$.personal')
from Employee
where ISJSON(preferences) = 1
and len(JSON_QUERY(preferences, '$.personal')) > 2

但是,此查询有效:

select JSON_QUERY(preferences, '$.personal')
from Employee
where ISJSON(preferences) = 1

另外,如果我:

create table tmp(name varchar(256), personal nvarchar(max))

insert into tmp 
select name, JSON_QUERY(preferences, '$.personal') as personal
from Employee
where ISJSON(preferences) = 1

select personal
from tmp
where len(personal) > 2

一切正常。

这是 JSON_QUERY() 的 SQL Server 错误还是我做错了什么?

(编辑)错误消息是:

JSON text is not properly formatted. Unexpected character 'n' is found at position 3.

(编辑)也失败了:

select JSON_QUERY(preferences, '$.personal')
from Employee
where ISJSON(preferences) = 1
and JSON_QUERY(preferences, '$.personal') is not null
4

1 回答 1

1

找到了解决方法;

with tmp as (
     select a.name, b.*
     from Employee a
     cross apply OPENJSON(preferences, '$.personal')
     where ISJSON(preferences) = 1
)
select t.[key]
from tmp t
where t.value is not null
and len(t.value) > 2

我很确定 JSON_QUERY() 的错误是 sql server 中的错误。希望它很快会在更新中得到修补:)

于 2019-03-25T19:17:31.997 回答