SQL Server Error 323: The COMPUTE Clause Is Not Allowed
In this article
SQL Server Error 323 occurs when you attempt to use the COMPUTE clause in a query against a modern version of SQL Server, typically SQL Server 2012 or later. This clause was deprecated and removed because it was replaced by more powerful and flexible alternatives like window functions and GROUP BY with ROLLUP or CUBE. The error is not a syntax mistake in the traditional sense—it's a deliberate restriction. To resolve this, you must rewrite your query using modern SQL constructs. The COMPUTE clause was used to generate summary rows at the end of result sets, but its functionality is now fully covered by window functions like SUM() OVER() and GROUPING SETS.
What You'll Need
- A working connection to a SQL Server instance (2012 or later)
- Access to a database where you can execute queries (at least READ permissions)
- A query that currently uses COMPUTE, COMPUTE BY, or related syntax
- Basic understanding of SQL SELECT statements and aggregation
Step-by-Step
- Identify the exact line in your query that uses COMPUTE or COMPUTE BY. Look for keywords like COMPUTE SUM(), COMPUTE AVG(), or COMPUTE BY column_name. These lines typically appear after the main SELECT statement and before any ORDER BY clause.
- Remove the entire COMPUTE or COMPUTE BY clause from your query. Do not try to comment it out—SQL Server will still parse it and throw the same error. Delete the line entirely.
- Replace the COMPUTE clause with a window function. For example, if your original query was: SELECT ProductID, Quantity, Price FROM Sales ORDER BY ProductID COMPUTE SUM(Quantity), AVG(Price);, rewrite it as: SELECT ProductID, Quantity, Price, SUM(Quantity) OVER() AS TotalQuantity, AVG(Price) OVER() AS AveragePrice FROM Sales ORDER BY ProductID;. The OVER() clause without partitioning applies the aggregate across the entire result set.
- If you used COMPUTE BY to generate subtotals for groups, replace it with GROUPING SETS or ROLLUP. For instance, if you had: SELECT Department, Employee, Salary FROM Employees ORDER BY Department COMPUTE SUM(Salary) BY Department;, rewrite it as: SELECT Department, Employee, SUM(Salary) AS Salary FROM Employees GROUP BY GROUPING SETS ((Department, Employee), (Department)) ORDER BY Department, Employee;. This will return both detail rows and department-level totals.
- Test the rewritten query in SQL Server Management Studio (SSMS) or another query tool. Execute it and verify that the output matches the original intent: summary values should appear alongside detail rows or in separate grouped rows as needed.
- Update any applications, reports, or scripts that rely on the old query. If the report or application expected a specific number of result sets (e.g., one for details and one for totals), you may need to adjust the consuming code to handle a single result set with summary columns.
- If you are using an older reporting tool like SQL Server Reporting Services (SSRS) that previously relied on COMPUTE, rebuild the report using tablix or matrix controls with built-in aggregation. Avoid embedding legacy SQL syntax in datasets.
Common Mistakes
- Trying to use COMPUTE in a view or stored procedure without realizing it’s blocked—views and procedures cannot contain COMPUTE clauses at all in modern SQL Server.
- Assuming COMPUTE BY still works for subtotals in newer versions, when it has been fully replaced by GROUPING SETS, ROLLUP, or CUBE.
- Replacing COMPUTE with a simple GROUP BY and losing detail rows. COMPUTE returned both detail and summary rows; GROUP BY only returns summary rows unless you use GROUPING SETS.
- Using SUM() OVER() but forgetting to include ORDER BY in the window function if you need ordered subtotals—though for total aggregates, ORDER BY is optional in OVER().
- Confusing COMPUTE with the deprecated COMPUTE clause in Sybase or older SQL Server versions and assuming the same syntax works everywhere.
Tips
- Use the SQL Server Migration Assistant (SSMA) if you’re migrating from Sybase or older SQL Server versions—it automatically detects and converts COMPUTE clauses to modern equivalents.
- When rewriting queries, start by testing the window function version on a small dataset to validate the math matches your old COMPUTE output.
- Window functions are more performant than COMPUTE and allow for multiple aggregations in a single pass, reducing server load.
- If you need to display totals at the bottom of a report, use the reporting tool’s built-in footer functionality instead of relying on SQL to generate them.
- Use the undocumented sys.dm_exec_query_stats DMV and sys.dm_exec_sql_text to find all queries containing COMPUTE across your server if you’re auditing legacy code.
- Consider using CTEs (Common Table Expressions) to separate detail and summary logic cleanly if your query becomes complex.
Troubleshooting
If the error persists after removing COMPUTE, check the following:
- Make sure you’ve deleted every instance of COMPUTE or COMPUTE BY in the query—sometimes multiple lines exist, especially in stored procedures with dynamic SQL.
- Verify that you’re not calling a view or function that contains COMPUTE internally. Use sp_depends or sys.sql_expression_dependencies to trace dependencies.
- If you’re using an ORM like Entity Framework or a third-party data access layer, ensure the generated SQL doesn’t include COMPUTE. Check the SQL profiler or logging output.
- Confirm your SQL Server version. If you’re on SQL Server 2008 R2 or earlier, COMPUTE is still allowed. If you upgraded recently, you may have inherited legacy code that needs updating.
- If the query works in SSMS but fails in an application, the application might be injecting COMPUTE via a template or configuration. Review the application’s SQL generation logic.
- Check for hidden characters or encoding issues in the query string—sometimes legacy scripts copy-paste invisible control characters that interfere with parsing.
Frequently Asked Questions
Can I use COMPUTE in SQL Server 2019?
No. The COMPUTE clause was completely removed after SQL Server 2008 R2. All versions from 2012 onward reject it with Error 323.
Is there a way to re-enable COMPUTE in newer SQL Server versions?
No. There is no registry setting, trace flag, or configuration option to re-enable COMPUTE. Microsoft removed it permanently for architectural and performance reasons.
What’s the best replacement for COMPUTE BY?
Use GROUPING SETS or ROLLUP. For example, GROUP BY ROLLUP(Department) returns both individual rows and subtotals per department in one result set.
Why did Microsoft remove COMPUTE?
COMPUTE was inefficient, limited to one result set, and couldn’t be used in views or subqueries. Window functions are more flexible, faster, and integrate better with modern SQL standards.
Can I use COMPUTE in Azure SQL Database?
No. Azure SQL Database is based on SQL Server 2012 or later codebase and does not support COMPUTE under any circumstances.
Do I need to rewrite all my old reports?
If your reports use COMPUTE clauses in their SQL datasets, yes. Update them to use window functions or let the reporting tool handle summarization. Most modern reporting tools like SSRS, Power BI, or Tableau handle aggregation better than legacy SQL.