Applies to: SQL Server 2025 (17.x) Preview
JSON 문서의 경로에서 SQL 값을 검색합니다.
Note
이 JSON_CONTAINS
함수는 현재 미리 보기 상태이며 SQL Server 2025(17.x) 미리 보기에서만 사용할 수 있습니다.
Syntax
JSON_CONTAINS( target_expression , search_value_expression [ , path_expression ] )
Arguments
target_expression
검색할 대상 JSON 문서를 반환하는 식입니다. The value can be a json type or character string value that contains a JSON document.
search_value_expression
An expression that returns a SQL scalar value or json type value to search in the specified SQL/JSON document.
path
JSON 문서의 검색 대상을 지정하는 SQL/JSON 경로입니다. 이 매개 변수는 선택 사항입니다.
You can provide a variable as the value of path. JSON 경로는 구문 분석을 위해 lax 또는 strict 모드를 지정할 수 있습니다. 구문 분석 모드를 지정하지 않으면 lax 모드가 기본값입니다. 자세한 내용은 JSON 경로 식을 참조하세요.
The default value for path is $
. As a result, if you don't provide a value for path, JSON_CONTAINS
searches for the value in the entire JSON document.
If the format of path isn't valid, JSON_CONTAINS
returns an error.
Return value
Returns an int value of 0
, 1
, or NULL
. 값은 1
지정된 검색 값이 대상 JSON 문서 내에 포함되었거나 0
그렇지 않은 경우를 나타냅니다. 함수는 JSON_CONTAINS
인수NULL
가 있거나 JSON 문서에서 지정된 SQL/JSON 경로를 찾을 수 없는 경우 반환 NULL
합니다.
Remarks
이 함수는 JSON_CONTAINS
값이 JSON 문서에 포함되어 있는지 검색하기 위해 다음 규칙을 따릅니다.
스칼라 검색 값은 비교할 수 있고 같은 경우에만 대상 스칼라에 포함됩니다. Since json types have only JSON number or string or true/false value, the possible SQL scalar types that can be specified as search value are limited to the SQL numeric types, character string types, and the bit type.
The SQL type of the scalar search value is used to perform the comparison with the json type value in the specified path.
JSON_VALUE
기반 조건자는JSON_VALUE
함수가 항상 문자열 값을 반환하는 경우와 다릅니다.검색 배열의 모든 요소가 대상 배열의 일부 요소에 포함된 경우에만 JSON 배열 검색 값이 대상 배열에 포함됩니다.
검색 값이 대상 배열의 일부 요소에 포함된 경우에만 스칼라 검색 값이 대상 배열에 포함됩니다.
검색 개체의 각 키/값이 대상 개체에 있는 경우에만 JSON 개체 검색 값이 대상 개체에 포함됩니다.
Limitations
함수를 JSON_CONTAINS
사용하는 경우 다음과 같은 제한 사항이 있습니다.
- The json type isn't supported as search value.
- 반환
JSON_QUERY
된 JSON 개체 또는 배열은 검색 값으로 지원되지 않습니다. - 경로 매개 변수는 현재 필요합니다.
- SQL/JSON 경로가 배열을 가리키는 경우 SQL/JSON 경로 식에 와일드카드가 필요합니다. 자동 배열 래핑 해제는 현재 첫 번째 수준에만 있습니다.
JSON 인덱스 지원에는 조건자와 다음 연산자가 포함됩니다 JSON_CONTAINS
.
- 비교 연산자(
=
) -
IS [NOT] NULL
서술어(현재 지원되지 않음)
Examples
A. JSON 경로에서 SQL 정수 값 검색
The following example shows how to search for a SQL int value in a JSON array in a JSON path.
DECLARE @j AS JSON = '{"a": 1, "b": 2, "c": {"d": 4, "ce":["dd"]}, "d": [1, 3, {"df": [89]}, false], "e":null, "f":true}';
SELECT json_contains(@j, 1, '$.a') AS is_value_found;
결과 집합은 다음과 같습니다.
is_value_found
--------
1
B. JSON 경로에서 SQL 문자 문자열 값 검색
다음 예제에서는 JSON 경로의 JSON 배열에서 SQL 문자 문자열 값을 검색하는 방법을 보여줍니다.
DECLARE @j AS JSON = '{"a": 1, "b": 2, "c": {"d": 4, "ce":["dd"]}, "d": [1, 3, {"df": [89]}, false], "e":null, "f":true}';
SELECT json_contains(@j, 'dd', '$.c.ce[*]') AS is_value_found;
결과 집합은 다음과 같습니다.
is_value_found
--------
1
C. JSON 경로의 JSON 배열에서 SQL 비트 값 검색
The following example shows how to search for a sql bit value in a JSON array in a JSON path.
DECLARE @j AS JSON = '{"a": 1, "b": 2, "c": {"d": 4, "ce":["dd"]}, "d": [1, 3, {"df": [89]}, false], "e":null, "f":true}';
SELECT json_contains(@j, CAST (0 AS BIT), '$.d[*]') AS is_value_found;
결과 집합은 다음과 같습니다.
is_value_found
--------
1
D. 중첩된 JSON 배열 내에 포함된 SQL 정수 값 검색
The following example shows how to search for a SQL int value contained within a nested JSON array in a JSON path.
DECLARE @j AS JSON = '{"a": 1, "b": 2, "c": {"d": 4, "ce":["dd"]}, "d": [1, 3, {"df": [89]}, false], "e":null, "f":true}';
SELECT json_contains(@j, 89, '$.d[*].df[*]') AS is_value_found;
결과 집합은 다음과 같습니다.
is_value_found
--------
1
E. JSON 배열의 JSON 개체 내에 포함된 SQL 정수 값 검색
The following example shows how to search for a SQL int value contained within a JSON object in a JSON array in a JSON path.
DECLARE @j AS JSON = '[{"a": 1}, {"b": 2}, {"c": 3}, {"a": 56}]';
SELECT json_contains(@j, 56, '$[*].a') AS is_value_found;
결과 집합은 다음과 같습니다.
is_value_found
--------
1