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

Troubleshooting Guide: SQL Server Error 4145

Why This Error Happens

Error 4145 (“An expression of non-boolean type specified in a context where a condition is expected”) occurs when T-SQL code uses a non-Boolean expression — such as an assignment, a scalar value, or a function returning a non-logical type — inside a control-of-flow statement like IF, WHILE, or CASE WHEN. This is commonly triggered by syntax carried over from older SQL Server versions or other database platforms (like MySQL or Oracle) that allow looser boolean coercion. It can also surface after upgrading to SQL Server 2025, since the updated query optimizer and parser enforce stricter ANSI SQL compliance on conditional expressions.

Step-by-Step Resolution Guide

Step 1: Identify the Offending Query or Procedure

  • Check the SQL Server error log or Activity Monitor for the exact batch that triggered Error 4145.
  • Run the failing query manually in SQL Server Management Studio (SSMS) connected to your SQL Server 2025 instance to capture the full error context, including line number.
  • Look specifically for IF, WHILE, CASE, or ON clauses where a variable or function result is used directly instead of a comparison.

Step 2: Correct the Expression Syntax

  • Rewrite any bare variable or assignment used as a condition. For example, change:
    IF @Status = 1 SET @Result = 1 (if mistakenly written as IF @Result = @Status) into an explicit comparison: IF @Status = 1.
  • Ensure functions returning INT, BIT, or VARCHAR are explicitly compared to a value, e.g., replace IF dbo.GetFlag(@ID) with IF dbo.GetFlag(@ID) = 1.
  • Update any legacy code migrated from MySQL/Oracle that relied on implicit truthy/falsy evaluation (e.g., WHILE @Counter should become WHILE @Counter > 0).

Step 3: Validate Compatibility Level Settings

  • Open SSMS, right-click the database, and go to Properties > Options > Compatibility Level.
  • Confirm the database is set to the correct level for SQL Server 2025 (160 or higher) to ensure the parser behavior matches documented standards.
  • If legacy applications depend on older parsing leniency, test in a staging environment before changing compatibility levels in production.

Step 4: Review Linked Stored Procedures, Triggers, and Functions

  • Use sp_helptext or script out all stored procedures, triggers, and user-defined functions associated with the failing operation.
  • Search for conditional blocks (IF, WHILE, CASE) and cross-reference each with Step 2’s correction pattern.
  • Recompile affected objects using EXEC sp_recompile 'ObjectName'; after making corrections.

Alternative or Verification Steps

  • Re-run the original failing query or application workflow in SSMS or Query Store to confirm Error 4145 no longer appears and execution completes successfully.
  • If the error persists in a third-party application, check for vendor-released hotfixes or compatibility patches for SQL Server 2025, as the stricter parser may require an updated application build.