SQL Server error 102: Incorrect syntax near
The T-SQL parser hit something it didn't expect. The quoted text is where parsing stopped; the real mistake is often just before it.
- Error number
102- Severity
- 15
- Category
- Syntax and compilation
What it means
SQL Server parses the whole batch before running any of it. Error 102 means the parser found a token that can't appear at that point and quotes it (Incorrect syntax near ','). The token named is where the parser gave up, which is usually right after the actual mistake: a missing comma, an unclosed parenthesis, or syntax from another database. Because parsing failed, nothing in the batch ran.
Common causes
- Syntax from another database:
LIMIT 10(MySQL, PostgreSQL), backtick-quoted names,||for string concatenation in older versions,CREATE TABLE IF NOT EXISTS. - A missing or extra comma in a column list, or a trailing comma before
FROMor). - Unbalanced parentheses or quotes.
- A statement that must be first in its batch, such as
CREATE VIEWorCREATE PROCEDURE, without aGObefore it. - A missing semicolon before
WITH(a CTE) orTHROW, which then get read as part of the previous statement. - Newer syntax on an older server or compatibility level, such as
STRING_AGGorCREATE OR ALTER.
How to fix it
-
Look at the text just before the quoted token and the line number in the message; that's where the mistake usually is.
-
Replace other dialects' syntax with T-SQL. For example, use
TOPorOFFSET ... FETCHinstead ofLIMIT, and square brackets or double quotes instead of backticks.SELECT TOP (10) * FROM dbo.Orders ORDER BY OrderDate DESC; SELECT * FROM dbo.Orders ORDER BY OrderDate DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;
-
End the previous statement with a semicolon before a CTE or
THROW.DECLARE @n int = 5; WITH recent AS (SELECT TOP (@n) * FROM dbo.Orders ORDER BY OrderDate DESC) SELECT * FROM recent;
-
Put
GObefore statements that must start a batch.CREATE TABLE dbo.T (id int); GO CREATE VIEW dbo.V AS SELECT id FROM dbo.T;
-
Check the database's compatibility level if newer syntax is rejected.
SELECT name, compatibility_level FROM sys.databases WHERE name = DB_NAME();
Reproduce it safely
SELECT * FROM sys.objects LIMIT 10; -- Msg 102: Incorrect syntax near 'LIMIT'.
Related errors
156Incorrect syntax near the keyword105Unclosed quotation mark after the string4145Non-boolean expression in a condition42601PostgreSQL: syntax error at or near1064MySQL: You have an error in your SQL syntaxORA-00933Oracle: SQL command not properly ended
More syntax and compilation errors
8120Column not in GROUP BY or aggregate8118Aggregate without GROUP BY8672MERGE touched the same row twice1934SET options have incorrect settings
The code and message text are from the Microsoft Database Engine events and errors reference. Browse the full SQL Server error list or search every database error.
Work on SQL Server in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQL Server and the other databases you use.