SQL Server Incorrect syntax near ''

cyej8jka  于 5个月前  发布在  其他
关注(0)|答案(9)|浏览(96)

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.

hwazgwia

hwazgwia1#

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.

i2loujxw

i2loujxw2#

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

  • From Encoding menu, now click "Encode in ANSI" and check again your text

You should easily find the wrong character(s)

agyaoht7

agyaoht73#

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)

2admgd59

2admgd594#

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.

6ljaweal

6ljaweal5#

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

ukqbszuj

ukqbszuj6#

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

6rvt4ljy

6rvt4ljy7#

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')"

yebdmbv4

yebdmbv48#

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.

abithluo

abithluo9#

Sometimes if your problem is not getting resolved from highly voted answers in this thread, do below. It may be the issue:

If you are using it in a system where it's a reserved keyword, you may need to encapsulate it with square brackets (for Microsoft SQL Server) or backticks (for MySQL) to avoid the syntax error

For example:

Select * from [User]

Select * from 'User'

相关问题