// SQL Server error reference

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
Error message
Incorrect syntax near '%.*ls'.

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 FROM or ).
  • Unbalanced parentheses or quotes.
  • A statement that must be first in its batch, such as CREATE VIEW or CREATE PROCEDURE, without a GO before it.
  • A missing semicolon before WITH (a CTE) or THROW, which then get read as part of the previous statement.
  • Newer syntax on an older server or compatibility level, such as STRING_AGG or CREATE OR ALTER.

How to fix it

  1. Look at the text just before the quoted token and the line number in the message; that's where the mistake usually is.

  2. Replace other dialects' syntax with T-SQL. For example, use TOP or OFFSET ... FETCH instead of LIMIT, 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;
  3. 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;
  4. Put GO before statements that must start a batch.

    CREATE TABLE dbo.T (id int);
    GO
    CREATE VIEW dbo.V AS SELECT id FROM dbo.T;
  5. 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

More syntax and compilation errors

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.

// fewer errors, faster fixes

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.