If you’re getting an error in SQL Server that reads “Incorrect syntax near the keyword ‘Order’“, it could be because you’re using the word Order
(or another reserved keyword) in your SQL code.
The 156 error itself merely indicates a syntax error, so there could be a multitude of reasons you might be getting this error. But when it refers to the keyword 'Order'
, this could suggest that you’re trying to use the word Order
as an identifier (such as a column name, table name, etc).
Given ORDER
is a reserved word in SQL, we would expect to get an error. That is, unless we properly delimit our identifier.
So to fix this issue, be sure to delimit any identifiers if they could conflict with reserved keywords.
Example of Error
Here’s an example of code that produces the error:
CREATE TABLE Order (
OrderID INT IDENTITY PRIMARY KEY,
OrderDate DATE,
Amount DECIMAL(10, 2),
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
Output:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'Order'.
This happened because I tried to create a table called Order
. In SQL Server, ORDER
is a reserved word, and so it threw an error.
We could get the same error if a table already exists of that name, and we try to insert data into it:
INSERT INTO Order (OrderDate, Amount, CustomerID) VALUES ('2024-01-02', 10.50, 1);
Output:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'Order'.
And the same again when selecting data from it:
SELECT * FROM Order;
Output:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'Order'.
Again, it’s a reserved word and so we get the error.
Solution
In SQL Server, when you need to use a reserved keyword like ORDER
as an identifier (such as a column name, table name, etc.), you have a couple of options:
- Square Brackets: You can enclose the keyword in square brackets, like
[Order]
. This is the most common way to handle reserved keywords in SQL Server. - Double Quotation Marks: Another option is to enclose the keyword in double quotation marks, like
"Order"
. This approach is ANSI SQL-compliant and works in SQL Server when theQUOTED_IDENTIFIER
setting isON
, which is the default setting in most environments.
Therefore, either of the following would work:
-- Using square brackets
CREATE TABLE [Order] (
OrderID INT IDENTITY PRIMARY KEY,
OrderDate DATE,
Amount DECIMAL(10, 2),
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
-- Using double quotes
CREATE TABLE "Order" (
OrderID INT IDENTITY PRIMARY KEY,
OrderDate DATE,
Amount DECIMAL(10, 2),
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
The practice of enclosing a reserved keyword (or any identifier that would otherwise cause a conflict) in square brackets or double quotes is generally referred to as “delimiting” the identifier. So when we do this, the identifiers are called “delimited identifiers”.
Using delimited identifiers allows you to safely use reserved keywords or special characters that would otherwise cause syntax errors in SQL. However, it’s generally advisable to avoid using reserved keywords as identifiers to prevent confusion and potential issues in the future.
With that in mind, another way to deal with the issue is to change the name of the entity, if possible. For example, we could change the table name to Orders
(plural) if your naming conventions allow it. Or perhaps CustomerOrder
if that suits you better.
So, the following code works without us having to delimit the table name:
CREATE TABLE Orders (
OrderID INT IDENTITY PRIMARY KEY,
OrderDate DATE,
Amount DECIMAL(10, 2),
CustomerID INT,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
Another reason to avoid using reserved keywords when creating objects in SQL is that it will also require any query that references that object to delimit the identifier.
So if we were to create the Order
table successfully (by delimiting the identifier), we would still get the error if we tried to insert data without delimiting the table name.
And if we were successful in inserting data (after delimiting the table name), we will still get an error if we try to select from that table without delimiting the identifier in our SELECT
statement:
SELECT * FROM Order;
Output:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'Order'.
So in this case we would need to delimit Order
in order to get the query to work:
SELECT * FROM [Order];
So as mentioned, probably better to avoid using reserved keywords as identifiers if possible.