Troubleshooting Guide: Error 160 – “Rule Does Not Contain a Variable”
Why This Error Happens
This error occurs when SQL Server 2025 parses a computed column, CHECK constraint, or default constraint rule definition that references a stored procedure syntax or an object without a valid @variable binding. It commonly surfaces after migrating legacy T-SQL rules (created with CREATE RULE) to newer bound-parameter validation logic introduced in SQL Server 2025, where the rule body fails to reference any parameter. It can also happen when a rule object was partially corrupted during a schema upgrade or replication sync, leaving a malformed definition in sys.syscomments or sys.sql_modules.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Rule Object
- Run the following query to locate rules missing variable bindings:
SELECT o.name, o.type_desc, m.definition
FROM sys.objects o
JOIN sys.sql_modules m ON o.object_id = m.object_id
WHERE o.type = 'R';
- Inspect the
definitioncolumn for any rule body that lacks an@prefixed variable (e.g.,CREATE RULE myRule AS value > 0instead ofCREATE RULE myRule AS @value > 0).
Step 2: Drop and Recreate the Rule with a Proper Variable
- Unbind the rule from any columns before dropping it:
EXEC sp_unbindrule 'dbo.TableName.ColumnName';
- Drop the broken rule:
DROP RULE dbo.myRule;
- Recreate it using a valid variable reference:
CREATE RULE dbo.myRule AS @value > 0;
Step 3: Rebind the Rule to the Target Column
- Reapply the corrected rule to the appropriate table/column:
EXEC sp_bindrule 'dbo.myRule', 'dbo.TableName.ColumnName';
- If the environment uses CHECK constraints instead (recommended in SQL Server 2025+), migrate the logic:
ALTER TABLE dbo.TableName
ADD CONSTRAINT CK_TableName_ColumnName CHECK (ColumnName > 0);
Step 4: Clear Cached Execution Plans
- Flush any stale plans referencing the old rule definition:
DBCC FREEPROCCACHE;
- Restart the SQL Server service via Services.msc (Windows Server 2025) or
net stop MSSQLSERVER && net start MSSQLSERVERto ensure metadata caches reload cleanly.
Alternative or Verification Steps
- Verify the fix by re-running the object insert/update that originally triggered Error 160; confirm no error is thrown and check
sys.messagesfor Error 160 recurrence in the SQL Server Error Log (ERRORLOGfile underC:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Log\). - As an alternative, if legacy
CREATE RULEobjects are no longer required, migrate all rule-based validations to CHECK constraints, since Microsoft has fully deprecated rule objects in favor of constraints starting with SQL Server 2025.