Troubleshooting Guide: SQL Server Error 128 — “The name is not permitted in this context”
Why This Error Happens
This error occurs when SQL Server’s query parser encounters a column name, variable, or identifier in a context where only constants, literals, or specific expressions are allowed—most commonly inside a DEFAULT constraint, a computed column definition, or a CHECK constraint referencing a non-deterministic or invalid expression. It also frequently arises when a subquery or column reference is mistakenly placed inside a GROUP BY, ORDER BY positional expression, or a CASE statement where SQL Server expects a static value instead of a dynamic reference. Essentially, the parser is telling you that the expression you supplied cannot be resolved at the point in execution where a constant is required, often due to scoping or evaluation-order issues in T-SQL.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Statement and Line
- Open the exact query or stored procedure referenced in the SQL Server error log or SSMS error output (the
%.*splaceholder will be replaced with the actual name in your real error message). - Use
SSMS > Query > Include Actual Execution Planor simply re-run the isolated statement to pinpoint which clause triggers Error 128. - Check if the issue occurs in a
DEFAULT,CHECK, computed column, orGROUP BY/ORDER BYclause.
Step 2: Correct the Expression Context
- If the error is in a DEFAULT constraint, replace any column reference with a literal constant or a deterministic system function (e.g.,
GETDATE(),0,'N/A') — column names are never valid here. - If the error is in a computed column, ensure you are not referencing another computed column that itself relies on non-deterministic logic; rewrite using base table columns only.
- If the error is in
GROUP BYorORDER BY, replace ordinal/positional references with explicit column names or valid expressions.
Step 3: Rebuild or Alter the Schema Object
- Run
ALTER TABLE [YourTable] DROP CONSTRAINT [ConstraintName];to remove the faulty constraint. - Recreate it with corrected syntax:
ALTER TABLE [YourTable] ADD CONSTRAINT [ConstraintName] DEFAULT ('ValidConstant') FOR [ColumnName]; - For stored procedures or views, use
CREATE OR ALTER PROCEDURE/VIEWafter fixing the invalid expression in SSMS.
Step 4: Validate Compatibility Level Settings
- On Windows Server 2025 / SQL Server 2025, confirm the database compatibility level isn’t causing older syntax to be misinterpreted: run
SELECT compatibility_level FROM sys.databases WHERE name = 'YourDB'; - If needed, update it:
ALTER DATABASE [YourDB] SET COMPATIBILITY_LEVEL = 170; - Restart the SQL Server service via
services.mscorRestart-Service MSSQLSERVERin PowerShell (run as Administrator) to apply changes cleanly.
Alternative or Verification Steps
- Re-execute the original query or trigger the stored procedure/application workflow that previously failed; confirm no Error 128 appears in SQL Server Management Studio or the Windows Event Viewer (Application Log).
- If the error persists, use
sp_helptext '[ObjectName]'to dump the full object definition and manually audit everyDEFAULT,CHECK, and computed column for embedded non-constant expressions.