Sql 如何使用like语句检查where子句中json数组的每个项

Sql 如何使用like语句检查where子句中json数组的每个项,sql,json,sql-server,tsql,sql-server-json,Sql,Json,Sql Server,Tsql,Sql Server Json,我正在为我的应用程序中的搜索字段创建一个查询。查询使用“like”关键字检查记录中的各种字段。其中一个字段是未命名的([{},{}])json数组。数组中的字段都匹配。我希望能够在不使用索引的情况下检查数组的每个“Value”属性,即“$[0].Value”。原因是阵列的大小可能会有所不同。以下是数据示例: [{ "MappedFieldName": "Customer", "DataType": "string", "Value": "Mapco Express" },

我正在为我的应用程序中的搜索字段创建一个查询。查询使用“like”关键字检查记录中的各种字段。其中一个字段是未命名的([{},{}])json数组。数组中的字段都匹配。我希望能够在不使用索引的情况下检查数组的每个“Value”属性,即“$[0].Value”。原因是阵列的大小可能会有所不同。以下是数据示例:

[{
    "MappedFieldName": "Customer",
    "DataType": "string",
    "Value": "Mapco Express"
}, {
    "MappedFieldName": "Invoice Nbr",
    "DataType": "string",
    "Value": "31856174"
}, {
    "MappedFieldName": "Invoice Document Date",
    "DataType": "DateTime",
    "Value": "2018-12-25 00:00:00.000"
}, {
    "MappedFieldName": "Processing Date",
    "DataType": "DateTime",
    "Value": "2019-01-04 00:00:00.000"
}, {
    "MappedFieldName": "Vendor Name",
    "DataType": "string",
    "Value": "Bullseye"
}, {
    "MappedFieldName": "Account Nbr",
    "DataType": "string",
    "Value": "0048219"
}, {
    "MappedFieldName": "Location #",
    "DataType": "string",
    "Value": "7520"
}, {
    "MappedFieldName": "Amount Invoiced",
    "DataType": "decimal",
    "Value": "3580.43"
}, {
    "MappedFieldName": "Amount Processed",
    "DataType": "decimal",
    "Value": "3580.43"
}, {
    "MappedFieldName": "Invoice Start Date",
    "DataType": "DateTime",
    "Value": "2018-04-01 00:00:00.000"
}, {
    "MappedFieldName": "Invoice End Date",
    "DataType": "DateTime",
    "Value": "2018-04-01 00:00:00.000"
}]

选择*
来自[dbo]。[发票]
其中JSON_值(InvoiceData,$.VALUE)类似于“%”++@searchText++“%”

此查询不起作用,因为我没有指定索引,即“$[0].Value”。

我找到了它。我首先在记录的json字段上使用OPENJSON,搜索文本过滤器用于获取json数组中文本所在部分的索引。接下来,我使用where子句中的索引来标识要查找的数组索引。下面是处理此问题的代码。这将返回在records json数组中找到搜索文本的所有记录

declare @searchText varchar(200) = '004'
declare @searchIndex varchar(10)

SELECT @searchIndex = [key]
FROM OPENJSON((SELECT InvoiceData FROM [dbo].[Invoice])) where Json_Value(value, '$.Value') like '%' + @searchText + '%'

SELECT *
    FROM [dbo].[Invoice]
    WHERE JSON_VALUE(InvoiceData, '$[' + @searchIndex +'].Value') like '%' + @searchText + '%'

这个答案可以简化。如果您有一个简单的答案,请随意发布。

输入的
JSON
是一个具有固定结构的
JSON
对象数组(
MappedFieldName
DataType
Value
键),因此另一种可能的方法是使用
OPENJSON()
with explicit schema返回一个表,其中包含在
with
子句中定义的列。通过这种方法,您可以过滤
发票
表和/或从输入的
JSON
中获取附加信息:

表:

CREATE TABLE Invoices (
   InvoiceData nvarchar(max)
)
INSERT INTO Invoices 
   (InvoiceData)
VALUES
   (N'[{ "MappedFieldName": "Customer", "DataType": "string", "Value": "Mapco Express"}, { "MappedFieldName": "Invoice Nbr", "DataType": "string", "Value": "31856174"}, { "MappedFieldName": "Invoice Document Date", "DataType": "DateTime", "Value": "2018-12-25 00:00:00.000"}, { "MappedFieldName": "Processing Date", "DataType": "DateTime", "Value": "2019-01-04 00:00:00.000"}, { "MappedFieldName": "Vendor Name", "DataType": "string", "Value": "Bullseye"}, { "MappedFieldName": "Account Nbr", "DataType": "string", "Value": "0048219"}, { "MappedFieldName": "Location #", "DataType": "string", "Value": "7520"}, { "MappedFieldName": "Amount Invoiced", "DataType": "decimal", "Value": "3580.43"}, { "MappedFieldName": "Amount Processed", "DataType": "decimal", "Value": "3580.43"}, { "MappedFieldName": "Invoice Start Date", "DataType": "DateTime", "Value": "2018-04-01 00:00:00.000"}, { "MappedFieldName": "Invoice End Date", "DataType": "DateTime", "Value": "2018-04-01 00:00:00.000"}]')
声明:

DECLARE @search nvarchar(max) = '004'
SELECT 
   i.*,
   -- You may include the keys and values from the input JSON:
   j.*
FROM Invoices i
CROSS APPLY OPENJSON(i.InvoiceData) WITH (
   -- You may define only the columns, that you need here:
   [MappedFieldName] nvarchar(100) '$.MappedFieldName',
   [DataType] nvarchar(20) '$.DataType',
   [Value] nvarchar(100) '$.Value' 
) j
WHERE j.[Value] LIKE CONCAT('%', @search, '%')

查询可以简单地编写如下。当对应的JSON列中有一个或多个匹配项时,它将返回1个发票:

SELECT *
FROM invoice
WHERE EXISTS (
    SELECT 1
    FROM OPENJSON(invoicedata)
    WITH (
        [Value] NVARCHAR(100) '$.Value'
    )
    WHERE [Value] LIKE '%' + '004' + '%'
)