SQL SERVER – Fix Error Msg 13602 working with JSON documents

My saga of working with JSON documents and finding out the various errors continues. With JSON being one of the new data structures introduced inside SQL Server 2016, I have been exploring to how these are fundamentally different from using the FORXML constructs. The more I have been working and testing with this capability, the more similarities I see in them. Coming from a generation of working with SQL Server for more than a decade, I try to explore and learn the new concepts keeping some reference. This helps in faster learning and can accelerate the way you work with the new concepts.

In this blog, I am calling out one of the restrictions of using the FOR JSON clause with SQL Server 2016. We cannot use this clause in a DML statement like UPDATE. You will be presented with an error as shown below:

Msg 13602, Level 16, State 1, Line 7
The FOR JSON clause is not allowed in a UPDATE statement.

The code that caused this error is shown below for your reference:

SET col = 1

As you can see the output from SQL Server Management Studio output:

Hence, to summarize, this error is returned if FOR JSON clause is used in a statement where it is not supported (e.g. UPDATE). Equivalent error is returned in FOR XML clause. For completeness of understanding, we will get an error as shown below when used with FOR XML clause:

Msg 6819, Level 16, State 1, Line 7
The FOR XML clause is not allowed in a UPDATE statement.

As you can see, many of these features of FOR JSON is similar to how FOR XML used to work in previous versions of SQL Server. I am sure none had any requirement of getting an output clause as JSON. But I could be proved wrong. Do let me know via comments on the scenario where you would have loved this to be enabled.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Exit mobile version