// SQL Server error reference

SQL Server error 156: Incorrect syntax near the keyword

A reserved keyword appeared where SQL Server didn't expect one, often a column or table named like a keyword, or a misplaced clause.

Error number
156
Severity
15
Category
Syntax and compilation
Error message
Incorrect syntax near the keyword '%.*ls'.

What it means

Error 156 is the keyword version of 102: the parser met a reserved word such as FROM, WHERE, ORDER, KEY or USER at a point where it doesn't fit. Either the keyword is being used as a name without quoting, or a clause is out of order or incomplete.

Common causes

  • A column or table named after a reserved word, such as User, Order, Key, Table or Index, used without brackets.
  • Clauses in the wrong order, such as ORDER BY before GROUP BY, or WHERE after GROUP BY.
  • A trailing comma right before FROM.
  • A derived table without an alias: SELECT * FROM (SELECT 1 AS x) needs AS t after the parenthesis.
  • ORDER BY inside a view, subquery or CTE without TOP or OFFSET.

How to fix it

  1. Quote names that are reserved words with brackets (or double quotes with QUOTED_IDENTIFIER ON).

    SELECT [User], [Order], [Key] FROM dbo.[Table];
  2. Give every derived table an alias.

    SELECT t.x FROM (SELECT 1 AS x) AS t;
  3. Put clauses in the standard order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.

Reproduce it safely

SELECT 1 AS x, FROM sys.objects;  -- Msg 156: Incorrect syntax near the keyword 'FROM'.

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.