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
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,TableorIndex, used without brackets. - Clauses in the wrong order, such as
ORDER BYbeforeGROUP BY, orWHEREafterGROUP BY. - A trailing comma right before
FROM. - A derived table without an alias:
SELECT * FROM (SELECT 1 AS x)needsAS tafter the parenthesis. ORDER BYinside a view, subquery or CTE withoutTOPorOFFSET.
How to fix it
-
Quote names that are reserved words with brackets (or double quotes with
QUOTED_IDENTIFIER ON).SELECT [User], [Order], [Key] FROM dbo.[Table];
-
Give every derived table an alias.
SELECT t.x FROM (SELECT 1 AS x) AS t;
-
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
102Incorrect syntax near105Unclosed quotation mark after the string42601PostgreSQL: syntax error at or near1064MySQL: You have an error in your SQL syntaxORA-00936Oracle: missing expression
More syntax and compilation errors
4145Non-boolean expression in a condition8120Column 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.