Incorrect syntax near 'go' in SQL Server Management Studio

38,496

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:

https://agilewebhosting.com/knowledgebase/63/SQL-Error-Incorrect-syntax-near-andsharp039GOandsharp039.html

Matrix

Share:
38,496
Eldritch Conundrum
Author by

Eldritch Conundrum

Apparently, this user prefers to keep an air of mystery about them. Oh well

Updated on November 18, 2020

Comments

  • Eldritch Conundrum
    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
    Damien_The_Unbeliever over 12 years
    The 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 your select 1 go example, go is a perfectly valid column alias, and the as keyword is not required.
  • Eldritch Conundrum
    Eldritch Conundrum over 12 years
    I 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
    andrew pate over 7 years
    This answer may be useful for .NET users running sql scripts with the SqlCommand class. See: stackoverflow.com/questions/18596876/…
  • gregsonian
    gregsonian over 2 years
    The 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.