Troubleshooting Guide: SQL Server Error 1018 — “Incorrect Syntax Near Table Hint”
Why This Error Happens
Error 1018 occurs when the SQL Server Query Parser encounters a malformed WITH clause, typically due to a missing comma between multiple table hints, an incorrectly quoted hint (e.g., NOLOCK instead of NOLOCK), or a deprecated hint syntax that is no longer supported in SQL Server 2025’s updated query engine. This is purely a T-SQL syntax validation failure and does not indicate database corruption or a server-level configuration issue. It most commonly surfaces after migrating legacy scripts, stored procedures, or ORM-generated queries (like older Entity Framework or Hibernate mappings) to a newer SQL Server compatibility level.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query
- Capture the full query text using SQL Server Profiler, Extended Events, or the error message pop-up in SSMS, which usually highlights the exact statement number.
- Check the Windows Event Viewer under Applications and Services Logs > MSSQLSERVER for the full stack trace tied to Event ID 1018.
- Look specifically for
WITH (...)clauses attached to table names inFROMorJOINsections.
Step 2: Correct the Table Hint Syntax
- Ensure every hint inside the parentheses is separated by a comma, for example:
FROM Orders WITH (NOLOCK, INDEX(IX_OrderDate)). - Remove deprecated hints such as
HOLDLOCKcombined improperly withREADPASTin older syntax formats; verify against the current Microsoft T-SQL reference for SQL Server 2025. - If the query was auto-generated by an ORM, update the ORM provider/driver to the latest version compatible with SQL Server 2025 (e.g., Microsoft.Data.SqlClient 6.x or newer).
Step 3: Validate Database Compatibility Level
- Open SSMS, right-click the target database, select Properties > Options, and check the Compatibility Level field.
- Run the following to confirm the current setting:
SELECT compatibility_level FROM sys.databases WHERE name = 'YourDatabaseName';- If the level is set below 160 (SQL Server 2022) or the new 2025 equivalent, some older hint syntax may behave unexpectedly; test the query after aligning it with the current compatibility level standard.
Step 4: Rebuild and Test the Query in Isolation
- Strip the query down to a minimal reproducible version containing only the
FROMandWITHclause. - Execute it directly in a new SSMS query window connected to the Windows Server 2025 instance to confirm the parser accepts the corrected syntax.
- Gradually reintroduce the rest of the original query (joins, filters, subqueries) to ensure no other clause is reintroducing the error.
Alternative or Verification Steps
- Run
SET PARSEONLY ON;before executing the corrected query batch — SQL Server will validate syntax without actually executing it, confirming the fix without touching live data. - If the error persists only on Windows Server 2025 but not on Windows 11 dev environments, verify both machines are running the identical SQL Server 2025 cumulative update (CU) via
SELECT @@VERSION;, as parser behavior can shift slightly between CU builds.