52

I'm trying to run the following fairly simple query in SQL Server Management Studio:

SELECT TOP 1000 * 
FROM 
    master.sys.procedures as procs
left join 
    master.sys.parameters as params on procs.object_id = params.object_id

This seems totally correct, but I keep getting the following error:

Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ''.

It works if I take out the join and only do a simple select:

SELECT TOP 1000 *
FROM 
    master.sys.procedures as procs

But I need the join to work. I don't even have the string '' in this query, so I can't figure out what it doesn't like.

halfer
  • 19,824
  • 17
  • 99
  • 186
Joshua Frank
  • 13,120
  • 11
  • 46
  • 95

8 Answers8

106

Such unexpected problems can appear when you copy the code from a web page or email and the text contains unprintable characters like individual CR or LF and non-breaking spaces.

halfer
  • 19,824
  • 17
  • 99
  • 186
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 5
    That was it. I went and deleted all white space manually and added spaces back, and now it works. – Joshua Frank Nov 04 '13 at 16:47
  • 1
    Copy pasting it into Notepad will reveal these special characters as well. Making it easier to remove them. – Peter Jul 28 '15 at 06:34
  • 1
    Not always. There is at least one SO question about string splitting where the problem was caused by a single Unicode character that appears as a comma followed by a space. – Panagiotis Kanavos Jul 28 '15 at 07:23
  • Yeah, mine was at the end of the line and still invisible in notepad. Pressing delete worked though. – Paul Totzke Nov 16 '17 at 21:20
  • I couldn't see the unprintable characters when I opened my script in SSMS or Notepad++ but when opening in VS Code they appeared. – Rich Feb 09 '23 at 17:45
24

Panagiotis Kanavos is right, sometimes copy and paste T-SQL can make appear unwanted characters...

I finally found a simple and fast way (only Notepad++ needed) to detect which character is wrong, without having to manually rewrite the whole statement: there is no need to save any file to disk.

It's pretty quick, in Notepad++:

  • Click "New file"
  • Check under the menu "Encoding": the value should be "Encode in UTF-8"; set it if it's not
  • Paste your text enter image description here
  • From Encoding menu, now click "Encode in ANSI" and check again your text enter image description here

You should easily find the wrong character(s)

MAXE
  • 4,978
  • 2
  • 45
  • 61
  • 1
    Thank you so much. Your solution easily revealed the source of my pain. One note, with my version of Notepad++, some strange characters became plain question marks. So I just searched for ? and deleted any I found in strange places. Thanks again. – Perry Tew Jan 30 '20 at 20:25
  • @PerryTew Maybe because a different encoding is set in the menu? – MAXE Jan 31 '20 at 08:04
4

You can identify the encoding used for the file (in this case sql file) using an editor (I used Visual studio code). Once you open the file, it shows you the encoding of the file at the lower right corner on the editor.

encoding

I had this issue when I was trying to check-in a file that was encoded UTF-BOM (originating from a non-windows machine) that had special characters appended to individual string characters

You can change the encoding of your file as follows:

In the bottom bar of VSCode, you'll see the label UTF-8 With BOM. Click it. A popup opens. Click Save with encoding. You can now pick a new encoding for that file (UTF-8)

3

The error for me was that I read the SQL statement from a text file, and the text file was saved in the UTF-8 with BOM (byte order mark) format.

To solve this, I opened the file in Notepad++ and under Encoding, chose UTF-8. Alternatively you can remove the first three bytes of the file with a hex editor.

1

I was using ADO.NET and was using SQL Command as:

 string query =
"SELECT * " +
"FROM table_name" +
"Where id=@id";

the thing was i missed a whitespace at the end of "FROM table_name"+ So basically it said

string query = "SELECT * FROM table_nameWHERE id=@id";

and this was causing the error.

Hope it helps

IamButtman
  • 307
  • 3
  • 15
0

I got this error because I pasted alias columns into a DECLARE statement.

DECLARE @userdata TABLE(
f.TABLE_CATALOG nvarchar(100),
f.TABLE_NAME nvarchar(100),
f.COLUMN_NAME nvarchar(100),
p.COLUMN_NAME nvarchar(100)
)
SELECT * FROM @userdata 

ERROR: Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '.'.

DECLARE @userdata TABLE(
f_TABLE_CATALOG nvarchar(100),
f_TABLE_NAME nvarchar(100),
f_COLUMN_NAME nvarchar(100),
p_COLUMN_NAME nvarchar(100)
)
SELECT * FROM @userdata

NO ERROR

DanielT
  • 53
  • 9
0

For me I was miss single quote in the statement

Incorrect One : "INSERT INTO Customers (CustomerNo, FirstName, MobileNo1, RelatedPersonMobileNo) VALUES ('John123', John', '1111111111', '1111111111)"

missed quote in John' and '1111111111

Correct One: "INSERT INTO Customers (CustomerNo, FirstName, MobileNo1, RelatedPersonMobileNo) VALUES ('John123', 'John', '1111111111', '1111111111')"

abdella
  • 490
  • 5
  • 11
0

I was able to run this by replacing the 'Dot'; with and 'Underscore'; for the [dbo][tablename].

EXAMPLE:
EXEC sp_columns INFORMATION_SCHEMA.COLUMNS GO //**this will NOT work. But will intelliSence/autocomplete as if its correct.

EXEC sp_columns INFORMATION_SCHEMA_COLUMNS GO //**This will run in Synapse. but funny enough will not autocomplete.

replace me with an underscore

enter image description here

using the dot, will autopoulate but after this go back and replace the dot with an underscore

user132992
  • 11
  • 2