Existing Data
{ "Attr1": "AAAAAAAAA", "Attr2": 70, "Attr3": null, "Attr4": false }
I want to insert in below format right after the Attr2
both exists
"NewAttr": { "SourceTable1.Column1": "12xx12", "SourceTable2.Column2": "0192xx" }
Column1 exists but not Column2
"NewAttr": { "SourceTable1.Column1": "12xx12" }
Column2 exists but not Column1
"NewAttr": { "SourceTable2.Column2": "0192xx" }
None exist
"NewAttr": {}
SELECT top 10000 JSON_MODIFY(dt.DestinationColumn, '$.NewAttr', st.Column1)
FROM dbo.Joiningtable jt
INNER JOIN dbo.DestinationTable dt ON dt.ID = jt.TID
INNER JOIN dbo.SourceTable st ON jt.TID = st.ID
I am not able to get the point to add nested attr as I showed in example.
I was trying to write the first select statement so that I can see results are correctly append. but as a final result, I need to update DestinationTable.DestinationColumn
2
Answers
You could either use nested
JSON_MODIFY
calls:Or you could generate the whole nested object in an
APPLY
and insert it at once:db<>fiddle
I used nested case statements
fiddle