SQL Server error 321 occurs when the database engine encounters a keyword or value in a table hint position that it does not recognize as a valid hint. To fix this, you must identify the incorrect keyword following the WITH clause or the table alias and replace it with a supported table hint or remove it entirely if it was added by mistake.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or your preferred SQL editor.
- The specific SQL query or stored procedure that is triggering the error.
- Administrative permissions to execute and test the modified query.
Step-by-Step
- Locate the Error Point: Run the problematic query. SQL Server will typically highlight the specific line or keyword that is causing the "not a recognized table hints option" error.
- Identify the Table Hint Section: Look for the
WITHkeyword followed by parentheses, or keywords placed immediately after a table name in aFROMorJOINclause. For example, a query likeSELECT * FROM Employees WITH (NOLOCK)uses a table hint. - Check for Syntax Errors: Ensure that the hint is enclosed in parentheses if using the
WITHsyntax. A common cause of error 321 is forgetting the parentheses, such as writingWITH NOLOCKinstead ofWITH (NOLOCK). - Verify the Hint Keyword: Compare the keyword you used against the official list of supported SQL Server table hints (such as
NOLOCK,ROWLOCK,TABLOCK, orREADPAST). If you have used a keyword from a different database system (like MySQL or Oracle) or a typo (e.g.,NOLOCKSinstead ofNOLOCK), the engine will throw error 321. - Remove Invalid Custom Hints: If you are attempting to use a "hint" that is actually a filter or a column name, move that logic to the
WHEREclause. Table hints are specifically for locking and indexing behavior, not for data filtering. - Test the Corrected Query: Replace the invalid keyword with the correct hint or remove the
WITH (...)block entirely. Execute the query to verify that the error is resolved.
Common Mistakes
- Missing Parentheses: Writing
FROM Table WITH NOLOCK. In modern versions of SQL Server, the correct syntax requires parentheses:WITH (NOLOCK). - Misplaced Commas: Adding a comma between the table name and the
WITHkeyword, which confuses the parser. - Confusing Hints with Joins: Placing a hint keyword where a
JOINtype (likeINNER JOINorLEFT JOIN) should be. - Using Deprecated Hints: Using hints that were supported in very old versions of SQL Server but have been removed or changed in current versions.
Tips
- Use Default Locking: Unless you have a specific performance reason to use
NOLOCK, avoid table hints. SQL Server's query optimizer usually chooses the most efficient locking strategy automatically. - Check Compatibility Levels: If a query worked on an older server but fails on a newer one, check the database compatibility level. Some hint behaviors change between versions.
- Use SSMS Intellisense: Pay attention to the red squiggly lines in SQL Server Management Studio; they often flag unrecognized keywords before you even run the query.
Troubleshooting
If the error persists after correcting the syntax, try the following:
- Simplify the Query: Remove all table hints from the query one by one. If the query runs without hints, you know exactly which specific hint was the culprit.
- Check for Hidden Characters: Sometimes copying and pasting code from a web page or document introduces non-printing characters or "smart quotes" that look like standard characters but are not recognized by the SQL engine. Retype the hint section manually.
- Verify Table Aliases: Ensure that if you are using a table alias, the hint follows the alias correctly. For example:
FROM Employees AS e WITH (NOLOCK).
Frequently Asked Questions
What is a table hint in SQL Server?
A table hint is a command that tells the SQL Server query optimizer to use a specific locking or indexing behavior for a table, overriding the default decision of the optimizer.
Is NOLOCK the most common cause of this error?
Yes, error 321 frequently occurs when NOLOCK is written without parentheses or misspelled, as it is the most commonly used table hint.
Can I use table hints in a View?
Generally, table hints are not allowed within the definition of a View. If you need specific locking behavior, you must apply the hint to the query that selects from the View, or use a stored procedure.
Does removing the hint affect data accuracy?
Removing a NOLOCK hint may increase locking (which can slow down a query in high-traffic environments) but it ensures you do not read "dirty data" (uncommitted transactions), which actually improves data accuracy.
Why does my query work on one server but give error 321 on another?
This usually happens due to different SQL Server versions or different Database Compatibility Levels. A hint recognized in SQL Server 2012 might be handled differently or require stricter syntax in SQL Server 2022.