Troubleshooting Guide: SQL Server Error 2217
Why This Error Happens
Error 2217 (“%s’%ls’ or ‘%ls’ expected”) is a T-SQL parser error that occurs when SQL Server’s query engine encounters invalid or malformed syntax it cannot interpret. This typically stems from mismatched brackets, missing keywords, incorrect delimiter usage, or corrupted script encoding (such as improper Unicode characters copied from external sources). It can also surface when deprecated syntax is used against SQL Server 2025’s updated parser, which has stricter grammar validation than previous versions.
Step-by-Step Resolution Guide
Step 1: Isolate the Offending Query
- Run the failing script in SQL Server Management Studio (SSMS) 20 or Azure Data Studio with “Include Actual Query Plan” disabled to get a clean parse-only error.
- Use
SET PARSEONLY ON;before executing the batch to confirm whether the error is purely syntactical. - Check the exact line number referenced in the error message output.
Step 2: Inspect and Correct Syntax Elements
- Verify all parentheses
(), brackets[], and quotation marks are properly closed and paired. - Look for missing commas, semicolons, or keywords like
AS,ON, orENDnear the reported line. - If using dynamic SQL, print the generated string with
PRINT @SQL;before execution to visually inspect for malformed concatenation.
Step 3: Check for Encoding or Copy-Paste Corruption
- Re-type suspicious lines manually instead of pasting from PDFs, Word documents, or web pages, as hidden Unicode characters (e.g., smart quotes “ ”) often trigger this error.
- Save the script file using UTF-8 encoding without BOM via Notepad++ or VS Code to eliminate hidden character conflicts.
- Open the file in a plain text editor to confirm no non-standard whitespace characters exist.
Step 4: Validate Against SQL Server 2025 Syntax Changes
- Cross-reference your script against the official Breaking Changes in SQL Server 2025 documentation, as some older T-SQL constructs (e.g., certain legacy
*=join syntax) are now fully deprecated. - Update any legacy syntax to ANSI-compliant equivalents (e.g., replace old-style outer joins with
LEFT JOIN/RIGHT JOIN).
Step 5: Rebuild the Script from a Known-Good Template
- If the script originated from an automated export or third-party tool, regenerate it using SSMS’s native “Script Table/Object As” feature.
- Compare the regenerated script line-by-line with the original to spot the exact divergence point.
Alternative or Verification Steps
- Execute the corrected script using
SQLCMD -S <ServerName> -i script.sql -o output.logand confirm the output log shows no parser errors. - If the error persists only in SSMS but not via
sqlcmd, the issue is likely a corrupted SSMS query cache — close SSMS, clear the%AppData%\Microsoft\SQL Server Management Studio\<version>\SqlStudio.bincache file, and reopen the script.