1

我正在将 JSON 数据保存在我的 SQL Server 2016 表中,并且我想通过在 JSON 上应用 where 子句来查询该表,如下所示。

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

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

无法调用 nvarchar(max) 上的方法。

4

2 回答 2

3

通过互联网后,我终于发现有一个名为 JSON_VALUE() 的方法应该用于查找 json 的内容。mssql 的语法是这样的。

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

于 2016-04-28T10:16:39.060 回答
1

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

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'
     )

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

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

在 SQL 中,完全限定的 SQL 列名通常是 like [DatabaseName].[schema].[tablename].[columnname],因此在 WHERE 子句中,SQL 将其解释c.customer_details.name.fullNamec.customer_details 列,就像c表别名一样。然后name.fullname看起来像对生成错误的列名的方法调用。

于 2016-04-21T11:43:51.780 回答