SQL Server Error 195 occurs when the database engine encounters a token—a word, symbol, or character—that it does not recognize as valid T-SQL syntax. This is almost always caused by a typo, a misplaced special character, or the use of a keyword that is not supported by your specific version of SQL Server. To fix this, you must identify the exact line where the error occurs and correct the syntax to match the official T-SQL standards.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or Azure Data Studio.
- The full error message, which typically includes the line number where the unrecognized token was found.
- Administrative permissions to execute and alter scripts on the target database.
Step-by-Step
- Locate the Error Line: Open the Messages tab in the results pane of your query window. Look for the line number associated with Error 195. SQL Server usually provides the specific line and character position where the "unrecognized token" was detected.
- Inspect for Special Characters: Examine the code around the flagged token. Look for invisible characters, "smart quotes" (curly quotes from Word or PDF documents), or non-standard dashes. SQL Server requires straight quotes (
'or") and standard hyphens. - Verify Keyword Spelling: Check the spelling of T-SQL keywords. A common cause is a typo in a command (e.g., writing
SLECTinstead ofSELECTorUPDATAinstead ofUPDATE). - Check Version Compatibility: If you are using a newer feature (such as a specific JSON function or a new window function), ensure your SQL Server version supports it. If you run a script designed for SQL Server 2022 on SQL Server 2016, the engine will flag the newer syntax as an unrecognized token.
- Validate Object Names: Ensure that table names, column names, or stored procedure names containing spaces or reserved words are properly enclosed in square brackets (e.g.,
[Order Details]). Without brackets, a space is interpreted as a token separator, and the second word becomes an "unrecognized token." - Clean the Script: If you copied the code from a website or document, paste it into a plain text editor (like Notepad) first to strip away hidden formatting characters before pasting it into SSMS.
- Execute the Corrected Query: Highlight only the corrected block of code and press F5 to execute.
Common Mistakes
- Copy-Pasting from Rich Text: Copying code from a blog or a Word document often introduces non-breaking spaces or stylized quotation marks that look correct to the eye but are invalid to the SQL compiler.
- Missing Brackets: Forgetting to wrap identifiers that start with numbers or contain special characters in
[]. - Incorrect Commenting: Using a single dash
-instead of the required double dash--for single-line comments, which leads the engine to try and parse the comment as a command. - Wrong Versioning: Attempting to use
STRING_AGGorJSON_VALUEon an older version of SQL Server that does not recognize those tokens.
Tips
- Use the Parser: In SSMS, look for the red squiggly lines in the editor. These are provided by the IntelliSense parser and often flag unrecognized tokens before you even run the script.
- Isolate the Code: If the script is long, comment out large sections and run small blocks one by one. This helps you pinpoint exactly which line is triggering Error 195.
- Check Collation: If you are using special characters in your naming conventions, ensure your database collation supports those characters.
Troubleshooting
If the error persists after you have corrected the spelling and removed special characters, try the following steps:
Verify the Connection: Ensure you are connected to the correct instance. If you are accidentally connected to an older version of SQL Server than intended, the syntax may be valid for your target but invalid for the instance you are currently hitting.
Update SSMS: Occasionally, the editor's IntelliSense may misidentify tokens. Ensure you are using the latest version of SQL Server Management Studio to get the most accurate syntax highlighting.
Test in a New Window: If you suspect hidden characters that you cannot see, open a completely new query window and manually type the problematic line instead of copying and pasting it.
Frequently Asked Questions
Why does the error say "not a recognized token" if the word is spelled correctly?
This usually happens because of hidden characters (like a non-breaking space) or because the word is a reserved keyword used in the wrong context, making the engine unable to parse it.
Can this error be caused by a database corruption?
No. Error 195 is a syntax/parsing error. It relates to the code being sent to the server, not the physical state of the data on the disk.
Does using square brackets [] always fix this?
It fixes the error if the "unrecognized token" is actually a valid table or column name that contains a space or a reserved word. It will not fix a misspelled keyword like SLECT.
Is this error different from a "Syntax Error"?
It is a specific type of syntax error. While a general syntax error might mean a missing comma, a "token" error means the engine found a specific string of characters that it doesn't know how to categorize as a command, operator, or identifier.
Will updating my SQL Server version fix this?
Only if the token you are using is a feature introduced in a newer version. If the error is caused by a typo or a special character, updating the software will not resolve the issue.