- Error number
- 105
- Severity
- 15 (user-correctable error)
- State
- 1
- Product
- Microsoft SQL Server Database Engine
- Versions
- All supported SQL Server versions (2016 to 2025), Azure SQL Database, Azure SQL Managed Instance
- Category
- Syntax & Object Resolution
- Last reviewed
- 23 October 2026
On this page
What it means
Error 105 means a string literal was opened with a single quote and never closed, so the parser reached the end of the batch while still inside the string.
In T-SQL a string literal starts at a single quote and ends at the next single quote that is not doubled. If the closing quote is missing, everything after the opening quote, including the rest of the line, later lines and the semicolon, becomes part of the string. When the parser reaches the end of the batch still inside that string, it raises 105 at severity 15 and the batch does not run. The text in quotes in the message is the unterminated string, which is why it can span several lines and contain your ORDER BY or semicolon.
The parser normally adds error 102 (incorrect syntax) for the same statement. The order tells you something: when 105 comes first, a closing quote is missing; when 102 comes first and names a word such as Brien, an apostrophe inside a value (O'Brien) closed the string early and the stray quote after it opened a new one. Inside dynamic SQL the line number is relative to the generated statement, not your script.
Common causes
- A closing quote missing at the end of a literal, often in a WHERE clause or VALUES list.
- An apostrophe inside a value (O'Brien, Don't) that was not doubled to two single quotes.
- Dynamic SQL built by concatenation where the code appends an opening quote but not the closing one.
- Hand-escaped dynamic SQL where one of the doubled quotes ('') is a single quote instead.
- A query copied from a document or chat that cut off the end of a string.
Diagnose it
Read-only These queries only read metadata and dynamic management views. They do not change data, permissions or configuration, so they are safe to run first.
Print the dynamic SQL instead of running it
For dynamic SQL, the error refers to the generated statement. Print it and read the quotes in the actual text.
DECLARE @name NVARCHAR(100) = N'Smith';
DECLARE @sql NVARCHAR(MAX) =
N'SELECT id FROM dbo.Customers WHERE name = ''' + @name;
PRINT @sql; The printed statement shows name = 'Smith with no closing quote, which is the string the 105 message quotes.
Count the quote characters in the statement
A valid statement contains an even number of single quotes. An odd count confirms an unclosed string.
DECLARE @name NVARCHAR(100) = N'Smith';
DECLARE @sql NVARCHAR(MAX) =
N'SELECT id FROM dbo.Customers WHERE name = ''' + @name;
SELECT LEN(@sql) - LEN(REPLACE(@sql, N'''', N'')) AS quote_count; An odd number (here 1) means a quote is missing. An even count with 102 errors points to a value whose apostrophe was not doubled.
Check the corrected statement compiles without running it
EXEC sp_describe_first_result_set
N'SELECT id FROM dbo.Customers WHERE name = ''O''''Brien'';'; A result row for the id column means the quotes are balanced; 105 or 102 means they are not.
How to fix it
Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.
Close the string
Read-onlyAdd the missing closing quote. Editors with T-SQL highlighting show the rest of the batch in string colour when a quote is open, which makes the spot easy to find.
SELECT id
FROM dbo.Customers
WHERE name = 'Smith'
ORDER BY id;
Double any apostrophe inside a value
Read-onlyInside a T-SQL literal, a single quote is written as two single quotes. This is not a double quote character.
SELECT id
FROM dbo.Customers
WHERE name = N'O''Brien';
Pass values as parameters instead of concatenating them
Changes dataWith sp_executesql parameters, values never become part of the SQL text, so apostrophes in data cannot break the statement and the code is not open to SQL injection. In application code, use the driver's parameters (for example SqlParameter in .NET) for the same reason.
DECLARE @name NVARCHAR(100) = N'O''Brien';
EXEC sp_executesql
N'UPDATE dbo.Customers SET city = N''Leeds'' WHERE name = @name;',
N'@name NVARCHAR(100)',
@name = @name;
Examples
A missing closing quote swallows the rest of the query
CREATE TABLE dbo.Customers (id INT PRIMARY KEY, name NVARCHAR(100));
SELECT id
FROM dbo.Customers
WHERE name = 'Smith
ORDER BY id;
Msg 105, Level 15, State 1, Line 5 Unclosed quotation mark after the character string 'Smith ORDER BY id; '. Msg 102, Level 15, State 1, Line 5 Incorrect syntax near 'Smith ORDER BY id; '.
Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.
Hand-escaped dynamic SQL with one quote too few
CREATE TABLE dbo.Customers (id INT PRIMARY KEY, name NVARCHAR(100), city NVARCHAR(50));
EXEC (N'SELECT id FROM dbo.Customers WHERE name = ''Smith'' AND city = ''Leeds');
Msg 105, Level 15, State 1, Line 1 Unclosed quotation mark after the character string 'Leeds'. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'Leeds'.
Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.
Concatenated dynamic SQL that never appends the closing quote
CREATE TABLE dbo.Customers (id INT PRIMARY KEY, name NVARCHAR(100), city NVARCHAR(50));
DECLARE @name NVARCHAR(100) = N'Smith';
DECLARE @sql NVARCHAR(MAX) =
N'UPDATE dbo.Customers SET city = N''Leeds'' WHERE name = ''' + @name;
EXEC (@sql);
Msg 105, Level 15, State 1, Line 1 Unclosed quotation mark after the character string 'Smith'. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'Smith'.
Reproduced on SQL Server 2025 (17.0.1145.4), 23 October 2026.
How to prevent it
Avoid building SQL text from values. Parameterised queries (sp_executesql parameters, or the parameters of your data access library) remove the whole class of quoting errors and close the door on SQL injection. See SQL injection explained.
When dynamic SQL is unavoidable, for example for object names, wrap identifiers with QUOTENAME, PRINT the statement while developing, and keep values as parameters.
Common questions
How do I put an apostrophe inside a string in SQL Server?
Write it twice: N'O''Brien' stores O'Brien. Double quotes do not work for this, because by default they delimit identifiers, not strings.
Why do I get both error 105 and error 102?
They come from the same mistake. The parser reports the unterminated string (105) and also cannot make sense of the statement around it (102). Fix the quotes and both go away.
The line number points to line 1 but my script has 50 lines. Why?
The error came from dynamic SQL run with EXEC or sp_executesql. Line numbers then refer to the generated statement, so print it to find the quote.
Learn more on CodeWithSQL
Official documentation
- Constants (Transact-SQL): character strings
- sp_executesql (Transact-SQL)
- QUOTENAME (Transact-SQL)
- Database engine events and errors (0 to 999)
Last technically reviewed 23 October 2026; examples and diagnostics run on SQL Server 2025 (17.0.1145.4). Message text checked against the engine's sys.messages catalogue.