Troubleshooting Guide: SQL Server Error 108
Why This Error Happens
Error 108 occurs when a ORDER BY clause references a column position number (e.g., ORDER BY 5) that exceeds the actual number of columns returned in the SELECT list. This is typically caused by a mismatch between a dynamically generated query and its underlying result set, often introduced after a schema change, a modified stored procedure, or an application update that altered the number of selected columns without updating the corresponding ORDER BY ordinal. It can also occur when copy-pasted T-SQL code retains an old ordinal reference from a query with more columns than the current one.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query
- Open SQL Server Management Studio (SSMS) on your Windows Server 2025 / Windows 11 instance and enable “Include Actual Execution Plan” or check the Application Event Viewer for the exact query text tied to Error 108.
- Use the following query against
sys.dm_exec_query_statsto locate recently failed queries by text pattern:
SELECT qt.text, qs.execution_count
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
WHERE qt.text LIKE '%ORDER BY%'
- Note the exact ordinal number referenced in the
ORDER BYclause (e.g.,ORDER BY 6).
Step 2: Count the Columns in the SELECT List
- Manually count every column, expression, and computed value listed between
SELECTandFROMin the flagged query. - Confirm whether the
ORDER BYordinal number is higher than this count — this confirms the root cause. - Check for recent
ALTER TABLE... DROP COLUMNor view modifications insys.objectsthat may have reduced the column count without updating dependent queries.
Step 3: Correct the ORDER BY Clause
- Edit the query or stored procedure to either reduce the ordinal number to a valid value within range, or replace the ordinal with the explicit column name for clarity and future stability.
- Example fix:
-- Incorrect
SELECT Name, Price FROM Products ORDER BY 3;
-- Corrected
SELECT Name, Price FROM Products ORDER BY Price;
- If the query is generated by an application (e.g., an ORM or reporting tool), update the query template or mapping file at the application layer, not just directly in SQL Server.
Step 4: Redeploy and Test the Corrected Object
- Save and redeploy the modified stored procedure, view, or application query using
sp_recompileif it’s a stored procedure:
EXEC sp_recompile 'dbo.YourStoredProcedureName';
- Restart the SQL Server Agent service or the affected application pool (via IIS Manager on Windows Server 2025) if the query is embedded in a scheduled job or web service.
Alternative or Verification Steps
- Run the corrected query directly in SSMS to confirm it executes without Error 108 and returns expected sorted results.
- As an alternative, refactor all legacy
ORDER BYordinal references across your codebase to use explicit column names, preventing recurrence when column counts change in the future.