Troubleshooting Guide: SQL Server Error 5362 — “Window ‘%.*ls’ is undefined”
Why This Error Happens
This error occurs when a T-SQL query references a named window (defined via the WINDOW clause or referenced in an OVER() clause) that either does not exist in the current query scope or was misspelled/mistyped during query construction. It is commonly triggered after upgrading to SQL Server 2025, which introduces stricter parsing and validation of the ANSI SQL WINDOW clause syntax used for named window specifications. Corrupted or partially migrated stored procedures, views, or ORM-generated queries that reference window definitions inconsistently can also cause this failure.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query and Window Reference
- Capture the exact query text from the error message, noting the window name shown in place of
%.*ls. - Run the query manually in SQL Server Management Studio (SSMS) or Azure Data Studio connected to your SQL Server 2025 instance to reproduce the error.
- Check the query for a
WINDOWclause definition (e.g.,WINDOW w AS (PARTITION BY ... ORDER BY ...)) and confirm the name used inOVER(w)matches exactly, including case sensitivity if the database collation is case-sensitive.
Step 2: Validate and Correct the Window Clause Syntax
- Ensure the named window is declared before it is referenced, typically placed after the
WHERE/GROUP BYclause and beforeORDER BY. - Confirm there are no typos, extra whitespace, or mismatched brackets in the window name declaration versus its usage in
OVER(). - If the query was auto-generated by an ORM (e.g., Entity Framework, Hibernate), regenerate the query or manually inspect the generated SQL for malformed window definitions.
- Example corrected syntax:
SELECT SalesPerson, SUM(Amount) OVER w AS TotalSales
FROM Sales
WINDOW w AS (PARTITION BY SalesPerson ORDER BY SaleDate)
Step 3: Check for Corrupted or Outdated Stored Procedures/Views
- Use
sp_helptext 'ProcedureName'orOBJECT_DEFINITION(OBJECT_ID('ViewName'))to inspect the stored object’s definition for inconsistent window references. - Drop and recreate the stored procedure or view with corrected syntax using
DROP PROCEDURE/CREATE PROCEDURE(or theALTERequivalent). - If the object was migrated from an older SQL Server version, verify compatibility level using:
SELECT compatibility_level FROM sys.databases WHERE name = 'YourDatabaseName';
- Update compatibility level if necessary with
ALTER DATABASE YourDatabaseName SET COMPATIBILITY_LEVEL = 160;(adjust to the appropriate level for SQL Server 2025).
Step 4: Apply Latest Cumulative Updates
- Navigate to Windows Server 2025 / Windows 11 Update settings or use SQL Server Setup to check for the latest Cumulative Update (CU) for SQL Server 2025, as parser-level bugs affecting named windows may be patched in later builds.
- Download and install the CU from the official Microsoft Download Center.
- Restart the SQL Server service via
services.mscornet stop MSSQLSERVER && net start MSSQLSERVERafter installation.
Alternative or Verification Steps
- Re-run the original failing query after corrections; confirm no Error 5362 appears and that result sets return as expected.
- As an alternative, avoid named windows entirely by inlining the
PARTITION BY/ORDER BYlogic directly inside eachOVER()clause, which bypasses the named window parser path completely.