Consider using OPENJSON for our JSON scalar access #30981
Labels
area-json
area-query
area-sqlserver
closed-no-further-action
The issue is closed and no further action is planned.
type-enhancement
Milestone
When using owned JSON entities, we currently use JSON_VALUE to extract scalars out of them. Since JSON_VALUE always returns nvarchar(4000), we apply a cast to the results based on the model type. For example:
... translates to:
There are issues with this translation:
The alternative translation would be to use OPENJSON with WITH instead:
In other words, every scalar access adds a CROSS APPLY to an OPENJSON invocation, applying the JSON conversion with WITH.
When the property being extracted is known to be a short string in the model (fitting in nvarchar(4000)), we could keep the current JSON_VALUE translation. When the type being extracted is incompatible with OPENJSON with WITH (e.g. geometry), we could do OPENJSON without WITH and then apply a relational cast (we should avoid JSON_VALUE still it truncates).
Note: consider the indexability of this technique, compared to JSON_VALUE (with computed columns)
The text was updated successfully, but these errors were encountered: