Troubleshooting Guide: SQL Server Error 2225 — “A string literal was expected”
Why This Error Happens
This error occurs when SQL Server’s T-SQL parser encounters a malformed string in a script, stored procedure, or dynamic SQL statement, typically due to unmatched or improperly escaped quotation marks. It is frequently triggered by dynamic SQL generation where single quotes inside a string are not properly doubled, or where a script was copied from an external source and contains smart quotes (curly quotes) instead of straight ASCII quotes. In rare cases, this can also surface from a corrupted or truncated script file being executed via sqlcmd or SQL Server Agent jobs on Windows Server 2025.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Script or Query
- Locate the exact batch that threw Error 2225 by checking SQL Server Agent job history or the application log that submitted the query.
- Open the script in SQL Server Management Studio (SSMS) and enable line numbers (Tools > Options > Text Editor > Transact-SQL > General).
- Run the batch in isolation to pinpoint the exact line referenced in the error message.
Step 2: Fix Quote Escaping in Dynamic SQL
- Search for any
EXEC(@sql)orsp_executesqlcalls and verify every embedded single quote is doubled (e.g.,'It''s a test'instead of'It's a test'). - Replace any curly/smart quotes (’ or ‘) with straight single quotes (‘) — these are often introduced when copying code from Word documents or PDFs.
- Use
QUOTENAME()orREPLACE(@value, '''', '''''')for user-supplied input being concatenated into dynamic SQL strings.
Step 3: Validate File Encoding and Source
- If the script originates from a
.sqlfile, re-save it using UTF-8 or ANSI encoding without BOM corruption via a plain text editor (e.g., Notepad++ or VS Code). - On Windows Server 2025, confirm the file wasn’t altered by an automated deployment tool that may have injected non-standard characters during transfer.
- Re-run the script through
sqlcmd -i scriptname.sqlto confirm the parser no longer throws Error 2225.
Step 4: Review Application-Generated Queries
- If the error originates from an application (e.g., via ODBC or OLE DB driver) rather than a manual script, enable SQL Server Profiler or Extended Events to capture the exact generated T-SQL statement.
- Check the application’s string-building logic for missing quote-escaping functions before the query reaches SQL Server 2025.
Alternative or Verification Steps
- After applying fixes, re-execute the corrected script in SSMS and confirm no red squiggly underlines or parser errors appear before running.
- As a quick alternative, wrap suspect string values in
QUOTENAME(@value, '''')to auto-handle escaping, then test the batch again in a non-production environment to confirm Error 2225 no longer appears.