我想查询 json inside 列的内容,如下所示

I want to query the content of the json inside column like below

我正在保存 JSON 数据我的 SQL 服务器 2016 table 我想通过在 JSON 上应用 where 子句来查询 table下面。

select c.customer_details.name.fullName 
from j_customer c 
where c.customer_details.name.fullName like '%Gopi%';

这在 oracle 中是可能的,但在 mssql 中它给出如下错误

Cannot call methods on nvarchar(max).

据我了解,您已使用如下查询将 JSON 数据存储到 table

declare @json nvarchar(max) = '"Customer_details":{"name":{"fullName":"Dhruv Joshi"}}'

 INSERT INTO j_customer
 SELECT * 
 FROM OPENJSON(@json)
 WITH (---some columns 
       fullName nvarchar(max) '$.Customer_details.name.fullName'
     )

在这种情况下,您可以简单地查询您的 table,如下所示

select c.fullName 
from j_customer c 
where c.fullName like '%Gopi%';

在 SQL 中,完全限定的 SQL 列名通常类似于 [DatabaseName].[schema].[tablename].[columnname],因此此处的 WHERE 子句 SQL 将 c.customer_details.name.fullName 解释为 c.customer_details 列,因为 c 是 table 别名。然后 name.fullname 看起来像是对产生错误的列名的方法调用。

上网后终于发现有一个叫JSON_VALUE()的方法可以用来查找json的内容。 mssql的语法是这样的

select * from j_customer where JSON_VALUE(c.customer_details, '$.name.fullName') = 'C';