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 FUNCTIONorRANGEclause and ensure it explicitly statesRANGE LEFT FOR VALUES (...)orRANGE RIGHT FOR VALUES (...). - Add the missing
FIRSTorLASTkeyword 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 isFETCH FIRST n ROWS ONLYorFETCH NEXT n ROWS ONLYrather 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
RANGEclause 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 FREEPROCCACHEto clear any cached malformed plans, then restart the SQL Server service viaservices.mscon Windows Server 2025/Windows 11 before retesting.