Incorrect syntax near 'go' in SQL Server Management Studio
Solution 1
SQL Server Management Studio can't handle some non printable characters.
Check the newline characters, probably you have Linux (LF) or Mac style (CR) instead of Windows style (CR and LF). You can check with any advanced text editor, for example Notepad++·
Solution 2
You opened a file in Mac format, with Carriage Returns ('\r') newlines.
The SQL parser behaves inconsistently on CR newlines. It supports them for some queries, like "select 1 go", but fails on others, like "drop function f go".
Convert all your sql files to windows encoding.
Solution 3
Been suffering with this problem mightily. Finally, used Notepad++.
Fixed by:
Format>Convert to UNIX
followed by
Format>Convert to Windows
Solution 4
You should remove all the "GO" from the script and it will resolve the issue.
Check this out for more info:
Matrix
Eldritch Conundrum
Apparently, this user prefers to keep an air of mystery about them. Oh well
Updated on November 18, 2020Comments
-
Eldritch Conundrum over 3 years
Executing the following SQL:
drop function f go
in MS Sql Server Management Studio give me this parse error:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'go'.
Why?
If I open a new tab and copy/paste the SQL into it, it also fails. But If I open a new tab and retype the SQL entirely, it works fine.
-
Damien_The_Unbeliever over 12 yearsThe SQL (actually, management studio) parser is consistent - in neither case does it interpret the gap before the
go
as a newline. It's just that, for yourselect 1 go
example,go
is a perfectly valid column alias, and theas
keyword is not required. -
Eldritch Conundrum over 12 yearsI now understand why "select 1 go" work. Since "select 1 from foo f" works and "select 1 from foo f go" fails, the syntax parsing issue is probably specific to the use of GO and the newline after it.
-
andrew pate over 7 yearsThis answer may be useful for .NET users running sql scripts with the SqlCommand class. See: stackoverflow.com/questions/18596876/…
-
gregsonian over 2 yearsThe UTF-8 character was never visible using the above techniques. SSMS was showing me with a very tiny red underline where the problems were. I was able to replace this unknown character with an empty string (no value) using Shift-Arrow > Ctrl-C > Ctrl-H > Ctrl-V. The SQL script then ran correctly.