How to fix Error 2351 on Windows Server 2025 / Windows 11

Why This Error Happens

Error 2351 is a T-SQL parser error that occurs when SQL Server encounters a RANGE clause (most commonly inside a CREATE PARTITION FUNCTION statement or an OFFSET/FETCH boundary clause) where it expects the keyword FIRST or LAST to define which side of the boundary value the partition belongs to, but finds an invalid token instead. This is typically caused by hand-edited or auto-generated DDL scripts (often produced by older SSDT/DACPAC tooling or migration scripts) that were written against an older T-SQL grammar and are missing the required keyword. It can also surface after an in-place upgrade to SQL Server 2025 if the database compatibility level has not been updated to match the new parser rules.

Step-by-Step Resolution Guide

Step 1: Locate the Offending Script

  • Open SQL Server Management Studio (SSMS) or Azure Data Studio and re-run the failing script in isolation to capture the exact line number referenced in the error output.
  • Check deployment logs under C:\Program Files\Microsoft SQL Server\160\DTS\Binn\ (or your SSDT output folder) if the script was generated via a DACPAC publish.

Step 2: Correct the Partition Function Syntax

  • Inspect the CREATE PARTITION FUNCTION or RANGE clause and ensure it explicitly states RANGE LEFT FOR VALUES (...) or RANGE RIGHT FOR VALUES (...).
  • Add the missing FIRST or LAST keyword where required, for example:
  CREATE PARTITION FUNCTION PF_Range (int)
  AS RANGE LEFT FOR VALUES (10, 20, 30);
  • If the script uses OFFSET...FETCH, confirm the correct syntax is FETCH FIRST n ROWS ONLY or FETCH NEXT n ROWS ONLY rather than a malformed hybrid.

Step 3: Validate Database Compatibility Level

  • Run the following to confirm the database is set to the SQL Server 2025 compatibility level (170):
  SELECT name, compatibility_level FROM sys.databases;
  ALTER DATABASE [YourDB] SET COMPATIBILITY_LEVEL = 170;
  • Re-run the script after updating the compatibility level, as older levels can cause the parser to reject newer or reorganized grammar paths.

Step 4: Update Deployment Tooling

  • If the script originates from SSDT/DACPAC, update Visual Studio/SSDT to the latest build that supports SQL Server 2025 T-SQL syntax, since outdated tooling can emit deprecated RANGE clause formats.
  • Re-generate and re-publish the DACPAC after the tooling update.

Alternative or Verification Steps

  • Re-execute the corrected script in a test database and confirm it completes without Error 2351, then verify partition boundaries with sys.partition_range_values.
  • If the error persists, run DBCC FREEPROCCACHE to clear any cached malformed plans, then restart the SQL Server service via services.msc on Windows Server 2025/Windows 11 before retesting.