SQL Server Error 1086: FOR XML and FOR JSON Fix
In this article
SQL Server error 1086 occurs when you use the FOR XML or FOR JSON clause in a query that also contains a syntax or structure SQL Server does not allow with those clauses. The fix is to isolate the FOR clause, correct the surrounding query, and ensure the clause is placed where SQL Server expects it. This guide walks through the practical steps to resolve it.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or Azure Data Studio, or another client that can run T-SQL.
- Permission to run and edit the query (and to alter the view, stored procedure, or function if the error originates there).
- The exact text of the failing query, including the FOR XML or FOR JSON clause.
- Basic familiarity with T-SQL SELECT syntax and subqueries.
Step-by-Step
- Reproduce the error in isolation. Run the failing query by itself in a new query window. If the error appears only when the query is called from an application or a stored procedure, copy the exact statement into SSMS and run it there. This confirms the error is in the T-SQL, not in how the client sends it.
- Read the full error message. Error 1086 typically reads: “The FOR XML and FOR JSON clauses are invalid in views, inline functions, derived tables, and subqueries when they contain a set operator.” The wording may vary slightly by SQL Server version, but the key phrase tells you where the FOR clause is being used illegally. Note the line number and the object name if one is given.
- Check for a set operator inside the same query block. The most common cause is combining FOR XML or FOR JSON with UNION, UNION ALL, EXCEPT, or INTERSECT in the same SELECT. For example,
SELECT ... FROM A UNION SELECT ... FROM B FOR JSON PATHis invalid. Rewrite it so the set operation happens in a subquery, and apply FOR JSON to the outer SELECT:SELECT * FROM (SELECT ... UNION SELECT ...) AS t FOR JSON PATH. - Move the FOR clause to the outermost SELECT. FOR XML and FOR JSON are only allowed at the top level of a query, not inside a view, inline table-valued function, derived table, or subquery that also contains a set operator. If you have
SELECT ... FROM (SELECT ... FOR JSON PATH) AS x, restructure it so the FOR clause is on the outer query, or remove it from the inner query and format the results in the application. - Check for FOR XML or FOR JSON inside a view or inline function. SQL Server does not allow these clauses in views or inline functions at all. If the error points to a view, open the view definition (in SSMS: Object Explorer > Databases > your database > Views > right-click the view > Script View as > CREATE To) and remove the FOR clause. Return the raw rows from the view and apply FOR JSON in the calling query instead.
- Look for a subquery that returns XML or JSON. A scalar subquery that uses FOR XML or FOR JSON can trigger error 1086 when it is combined with a set operator in the outer query. Rewrite the subquery to return the raw data and format it outside, or use a JOIN and apply the FOR clause once at the end.
- Simplify and rebuild. If the query is large, comment out the FOR clause and run the rest. Then add the FOR clause back on the outermost SELECT only. This confirms the rest of the query is valid and isolates the clause placement.
- Test the corrected query. Run it and verify the output is the XML or JSON you expect. Check that column aliases and nested structures still make sense after moving the clause. If the query is used by an application, update the application’s SQL text or the stored procedure definition to match.
- Update dependent objects. If the query lives in a stored procedure, view, or function, use ALTER (not CREATE) to apply the fix. For a view,
ALTER VIEW; for a stored procedure,ALTER PROCEDURE. Then refresh the object in SSMS and rerun the application to confirm the error is gone.
Common Mistakes
- Putting FOR JSON inside a subquery that also uses UNION. The clause must be on the outermost SELECT. Moving it inside a derived table almost always recreates the error.
- Assuming the error is a version bug. Error 1086 is a syntax and placement rule, not a bug. It appears in supported SQL Server versions and Azure SQL Database; the fix is rewriting the query.
- Leaving FOR XML in a view. Views cannot contain FOR XML or FOR JSON. Remove it from the view and format in the application or in a stored procedure that calls the view.
- Forgetting to update the application’s query text. Fixing the query in SSMS does not fix the application if it sends its own copy of the SQL. Update the source code or the stored procedure the app calls.
- Using FOR JSON AUTO with complex joins without checking output. AUTO mode nests results based on the SELECT order; after restructuring, the JSON shape can change. Verify the output structure before deploying.
Tips
- Keep FOR XML and FOR JSON at the very end of the outermost SELECT. This makes the query easier to read and avoids most placement errors.
- When you need to combine result sets, do the UNION or UNION ALL in a subquery and apply FOR JSON to the outer query. This is the standard workaround for error 1086.
- Use
FOR JSON PATHrather thanFOR JSON AUTOwhen you need control over column names and nesting; PATH gives you explicit aliases. - If the output is consumed by an API, consider returning tabular data and letting the application serialize it to JSON. This avoids the FOR clause entirely and is often easier to maintain.
- Test changes on a development copy of the database before altering production views or procedures.
Troubleshooting
If the error persists after moving the FOR clause to the outer SELECT, check for nested set operators. A query like SELECT ... FROM (SELECT ... UNION SELECT ...) AS a UNION SELECT ... FOR JSON PATH still has a set operator at the same level as the FOR clause. Wrap the entire set operation in a derived table and apply FOR JSON outside: SELECT * FROM ( ... ) AS combined FOR JSON PATH.
If the error names a view or function, script the object and search for “FOR XML” or “FOR JSON”. Remove the clause from the object definition and apply it in the calling query. If the object is an inline table-valued function, you cannot use FOR XML or FOR JSON inside it at all; return the rows and format outside.
If the query runs in SSMS but fails from an application, the application may be wrapping your query in another SELECT or adding a set operator. Check the application’s query builder or ORM settings. Some ORMs add FOR JSON automatically; disable that and serialize in the application layer instead.
If you are unsure which part of a long query triggers the error, comment out sections from the inside out. Start with the innermost subquery and work outward until the error disappears; the last section you re-enabled contains the problem.
Frequently Asked Questions
What exactly does SQL Server error 1086 mean?
It means a FOR XML or FOR JSON clause is used where SQL Server does not allow it, most often in a query block that also contains UNION, EXCEPT, or INTERSECT, or inside a view, inline function, derived table, or subquery.
Can I use FOR JSON with UNION?
Not directly in the same SELECT. Put the UNION in a subquery and apply FOR JSON to the outer SELECT that reads from that subquery.
Why does the query work in SSMS but fail in my application?
The application may be adding its own set operator or wrapping your query. Check the generated SQL and ensure FOR JSON is only on the outermost statement.
Does error 1086 affect both FOR XML and FOR JSON?
Yes. The same placement rules apply to both clauses, and the error message references both.
Can I use FOR XML inside a view?
No. Views and inline table-valued functions cannot contain FOR XML or FOR JSON. Remove the clause from the object and format the output in the calling query or application.
What is the quickest fix for error 1086?
Move the FOR XML or FOR JSON clause to the outermost SELECT and wrap any set operation in a subquery. If the clause is inside a view or function, remove it there and apply it in the caller.