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

Why This Error Happens

Error 4518 occurs when a user or application attempts to execute DDL operations (such as ALTER, DROP, or GRANT with modification intent) against a system-generated statistics object or optimizer view, typically represented internally as objects like sys.stats auto-created views or filtered statistics views. SQL Server 2025 automatically creates these views to support features like automatic indexing recommendations, cardinality estimation, and Intelligent Query Processing (IQP), and it locks them from direct user modification to preserve optimizer integrity. This commonly surfaces after a database migration, an in-place upgrade to SQL Server 2025, or when a maintenance script written for older engine versions tries to manage objects that are now reserved by the Query Optimization Engine.

Step-by-Step Resolution Guide

Step 1: Identify the Offending Object and Script

  • Capture the full error text, noting the %.*ls placeholder value — this is the exact system view name (often prefixed with _WA_Sys_ or _dta_stats).
  • Run the following to locate the object definition and confirm its system-generated status:
  SELECT object_id, name, is_ms_shipped 
  FROM sys.objects 
  WHERE name = '<ObjectNameFromError>';
  • If is_ms_shipped = 1, this confirms the view is protected and cannot be altered or dropped by standard T-SQL commands.

Step 2: Correct the Maintenance Script or Job

  • Open SQL Server Agent (via SSMS > SQL Server Agent > Jobs) or the deployment script that triggered the error.
  • Remove or comment out any ALTER VIEW, DROP VIEW, or sp_rename commands targeting system-generated optimizer views.
  • If the script originates from a third-party indexing/maintenance tool (e.g., Ola Hallengren’s scripts), update to the latest version compatible with SQL Server 2025, as older versions may not exclude is_ms_shipped objects in their WHERE clauses.

Step 3: Rebuild Statistics Instead of Manual Edits

  • Instead of manually modifying the view, let the engine manage it by updating statistics at the table level:
  UPDATE STATISTICS dbo.YourTableName WITH FULLSCAN;
  • This refreshes the underlying data the system view references without attempting to touch the protected object directly.

Step 4: Validate Database Compatibility Level

  • Open SSMS, right-click the database > Properties > Options, and confirm the Compatibility Level is set to 170 (SQL Server 2025) if the database was upgraded from an older instance.
  • Run:
  ALTER DATABASE [YourDB] SET COMPATIBILITY_LEVEL = 170;
  • A mismatched compatibility level after an in-place upgrade can cause legacy scripts to incorrectly target new auto-generated optimizer objects.

Alternative or Verification Steps

  • Verify the fix by re-running the original job or script; monitor the SQL Server Error Log (via EXEC sp_readerrorlog) to confirm Error 4518 no longer appears.
  • If the issue persists, use Extended Events to trace the exact statement (sqlserver.error_reported filtered on Error 4518) generating the call, as it may originate from an ORM framework (e.g., Entity Framework) issuing unexpected schema-modification queries.