Troubleshooting Guide: Error 11722 – “NEXT VALUE FOR function is not allowed in the WHEN MATCHED clause”
Why This Error Happens
This error occurs because SQL Server explicitly restricts the use of the NEXT VALUE FOR sequence function inside the WHEN MATCHED clause of a MERGE statement, as this could cause non-deterministic sequence consumption during row-matching evaluation. The restriction exists to prevent the sequence generator from advancing unpredictably when SQL Server internally re-evaluates matched rows. This is a T-SQL syntax/design limitation rather than a permissions, port, or corruption issue, meaning the query logic itself must be restructured rather than “repaired.”
Step-by-Step Resolution Guide
Step 1: Identify the Offending MERGE Statement
- Open SQL Server Management Studio (SSMS) on your Windows Server 2025 / Windows 11 instance and locate the stored procedure, trigger, or ad-hoc script that triggered Error 11722.
- Search specifically for
NEXT VALUE FOR [SequenceName]text occurring within theWHEN MATCHED THEN UPDATE SETblock of aMERGEstatement. - Note the column(s) being assigned the sequence value for use in the next step.
Step 2: Move Sequence Generation Outside the MERGE Statement
- Declare a table variable or temporary table (e.g.,
DECLARE @StagingData TABLE (...)) prior to theMERGEcall. - Populate this staging table first, generating the sequence value using
SELECT NEXT VALUE FOR [SequenceName] AS NewSeqVal, * FROM [SourceTable]. - Rewrite the
MERGEstatement to reference the pre-computedNewSeqValcolumn from the staging table instead of callingNEXT VALUE FORdirectly insideWHEN MATCHED. - Example pattern:
MERGE TargetTable AS T
USING @StagingData AS S
ON T.ID = S.ID
WHEN MATCHED THEN
UPDATE SET T.SeqColumn = S.NewSeqVal;
Step 3: Validate Sequence Object Permissions and Scope
- Confirm the executing login has
UPDATEpermission on the sequence object:GRANT UPDATE ON OBJECT::dbo.YourSequence TO [YourUser]; - Check that the sequence isn’t being called redundantly in both
WHEN NOT MATCHEDand the staging query, which can cause gaps or double-increments. - Review Dynamic Management View
sys.sequencesto confirm current value and increment settings align with expected business logic.
Step 4: Deploy and Test the Corrected Script
- Save the revised stored procedure or script.
- Execute the script in a non-production/staging instance of SQL Server 2025 first to confirm Error 11722 no longer appears.
- Check SQL Server Error Logs (via
EXEC sp_readerrorlogor Event Viewer under Applications and Services Logs > MSSQLSERVER) to confirm no residual errors are logged.
Alternative or Verification Steps
- Verification: Run the corrected
MERGEstatement against a test dataset containing duplicate/matching keys and confirm the sequence column increments exactly once per row without throwing Error 11722. - Alternative: If restructuring the
MERGEis not feasible, replace theMERGEstatement entirely with separateUPDATEandINSERTstatements, applyingNEXT VALUE FORdirectly within the standaloneUPDATEstatement’sSETclause, since this restriction only applies withinMERGEsyntax.