I have a table that with a column Info VARCHAR(MAX)
constrained to always be valid json. What is the best way to query a key with JSON_QUERY/JSON_VALUE if I don't know ahead of time if the value associated with the key is a scalar or not? Currently I am returning where either has a value as shown below.
SELECT ID, JSON_VALUE(Info, '$.Key') as json_val, JSON_QUERY(Info, '$.Key') as json_query
FROM TABLE
WHERE (JSON_VALUE(Info, '$.Key') IS NOT NULL OR JSON_QUERY(Info, '$.Key') IS NOT NULL)
and relying on the fact that the results are mutually exclusive.
The problem is the JSON_QUERY
in the WHERE
clause prevents any indexes on the virual column vKey AS JSON_VALUE(Info, '$.Key')
from being used.