SQL Server Error 485: Views and Inline Functions Cannot Return XML
In this article
SQL Server error 485 occurs when you try to create or alter a view or an inline table-valued function that returns an XML data type column. The database engine blocks this because views and inline functions must return a relational result set, and XML columns are not allowed in their output. The fix is to restructure your query so the XML is generated after the view or function, or to use a multi-statement table-valued function instead.
What You'll Need
- SQL Server Management Studio (SSMS) or another query editor with permission to create or alter objects in the target database.
- ALTER or CREATE permission on the schema where the view or function lives.
- Read permission on the underlying tables or views you are querying.
- Access to the query that is causing error 485, or the ability to write a new one.
Step-by-Step
- Reproduce the error to confirm the cause. Run the offending CREATE VIEW or CREATE FUNCTION statement. You will see error 485 with a message like "Views and inline functions cannot return xml." Note the exact line or column where the XML expression appears.
- Identify the XML-producing expression. Look for FOR XML clauses, or functions like
CONVERT(XML, ...),CAST(... AS XML), orXMLDATAin the SELECT list. These are what trigger the error. - Remove the XML conversion from the view or inline function. Change the SELECT list so it returns only standard relational columns (integers, strings, dates, etc.). For example, if you had
SELECT CONVERT(XML, ', change it to') AS XmlColumn SELECT 'or drop that column entirely.' AS XmlText - If you need the XML result, move the conversion outside. Create the view or inline function without the XML, then wrap it in an outer query that performs the conversion. For instance:
CREATE VIEW dbo.MyView AS SELECT OrderID, OrderDate FROM dbo.Orders;
Then run:SELECT OrderID, (SELECT OrderDate FOR XML PATH('order')) FROM dbo.MyView;
This works because the view returns relational data, and the XML is generated only when you query it. - If you must return XML directly from a function, use a multi-statement table-valued function. Unlike inline functions, multi-statement functions can return XML columns. Define a table variable with an XML column, fill it, and return it. Example:
CREATE FUNCTION dbo.GetOrdersXml() RETURNS @Result TABLE (XmlData XML) AS BEGIN INSERT INTO @Result (XmlData) SELECT (SELECT OrderID, OrderDate FROM dbo.Orders FOR XML PATH('order')); RETURN; END;
This is the only function type that supports XML output. - For views, consider an indexed view or a stored procedure as alternatives. If you need to query the XML frequently, a stored procedure can return XML directly. An indexed view cannot contain XML columns either, so it won't help here.
- Re-test after the change. Run the CREATE or ALTER statement again. It should succeed. Then query the object to verify the results are correct.
Common Mistakes
- Using FOR XML in the view definition. Even if you alias the column, FOR XML produces an XML fragment that the view engine rejects. Remove the clause entirely.
- Confusing inline and multi-statement functions. Inline functions are written as a single SELECT statement and cannot have XML columns. Multi-statement functions have a BEGIN...END block and can. If you tried to add XML to an inline function, you must rewrite it as multi-statement.
- Forgetting that the error also applies to ALTER. You might see error 485 when modifying an existing view or function, not just when creating new ones. The same rules apply.
- Assuming the error is about the data itself. Error 485 is about the column type, not the content. Even an empty XML string like
CAST('' AS XML)will fail.
Tips
- Use a stored procedure for complex XML generation. Stored procedures allow full control over XML construction with FOR XML and can return multiple result sets. They are often a better fit than a view when XML is the goal.
- Keep views simple. Views are meant to encapsulate relational logic. If you find yourself needing XML in a view, step back and ask if a procedure or function would be cleaner.
- Test with a simple query first. Before building a large view, test the XML conversion in a plain SELECT to ensure it works and returns the shape you want.
- Use explicit column lists. Avoid
SELECT *in views and functions. It makes the schema clearer and prevents accidental XML columns from sneaking in. - Check the compatibility level. Older databases might have different XML behaviors. Ensure your database compatibility level is set to a modern value (like 130 or higher) if you need advanced XML features, but note that error 485 applies across all levels.
Troubleshooting
If the error persists after removing FOR XML: Look for implicit XML conversions. Functions like CONVERT(XML, column) or CAST(column AS XML) also trigger the error. Search the SELECT list for any XML keyword. Also check computed columns in the underlying tables—if a column is already XML type, selecting it directly will cause the error.
If you cannot modify the view because other objects depend on it: You can drop the dependent objects temporarily, alter the view, and recreate them. Use sp_depends or query sys.sql_expression_dependencies to find dependencies. Be careful to script out the dependent objects first so you can recreate them exactly.
If the multi-statement function is slow: Multi-statement functions can have performance issues because they use table variables. Consider using an inline function that returns relational data and then apply FOR XML in the outer query. That pattern often performs better.
If you get a different error after the change: Error 485 is specific to XML columns. If you now see errors about data types or conversions, check that your outer query handles the XML correctly. Use FOR XML PATH or FOR XML AUTO with proper root element if needed.
If the view is part of a schema-bound object: Schema binding prevents changes to underlying objects. You may need to alter the view to remove schema binding first, then make your changes, then reapply binding. This is uncommon but possible.
Frequently Asked Questions
Can a view return XML in any SQL Server version?
No. The restriction has been consistent across all SQL Server versions. Views are relational objects and cannot have XML columns.
Can an inline table-valued function return XML if I use a different syntax?
No. Inline functions are essentially parameterized views. They follow the same rule. You must use a multi-statement function to return XML directly.
Is there a workaround using a common table expression (CTE)?
A CTE is not a persisted object, so it does not trigger error 485. You can use a CTE to generate XML in a query without creating a view or function. The error only applies to CREATE/ALTER VIEW or CREATE FUNCTION.
Can I use FOR XML in a view if I wrap it in a subquery?
No. The view's SELECT list still contains the XML output, even if nested. The entire view definition is parsed and the XML column is detected. You must move the FOR XML outside the view.
What is the difference between error 485 and error 486?
Error 486 is about text, ntext, or image columns in views. Error 485 is specifically about XML. Both are restrictions on data types in views. The fix is similar: remove the restricted type from the view definition.
Can I use an XML schema collection in a multi-statement function?
Yes. You can declare an XML column with a schema collection in the table variable of a multi-statement function. This gives you typed XML output. Just ensure the schema collection exists in the database.