Search code examples
sqlsql-serverwindowst-sqlsyntax-error

Alter table with GO results in syntax error


I have been scratching my head for a while with this error.

The following script:

alter table t alter column c int
GO

Results in error:

Incorrect syntax near 'GO'.

What I mainly want is to embed this script inside of a longer script hence the need to execute the transaction before the rest of the script proceeds. I have also tried it with and without semicolons and that makes no difference.

I would appreciate any kind guidance.

Same error in SQL server 2016 and 2019. I am running SQL in SSMS interactively. The existence of the table does not make a difference to the error. I have tried it with the following DDL:

create table t (c float)

screenshot is here.enter image description here

screenshow with SELECT then GO showing that other statements with GO work fine. enter image description here


Solution

  • I was able to reproduce the issue with a SQL file that uses Macintosh line endings (CR instead of CR LF or LF):

    The document opened in Notepad++

    Query opened and executed in SSMS

    The solution is to convert the line ending to Windows as described here.