Incorrect syntax near 'GO'

Andrew Bullock picture Andrew Bullock · Sep 5, 2014 · Viewed 68.6k times · Source

How can I execute the following SQL inside a single command (single execution) through ADO.NET?

ALTER TABLE [MyTable]
    ADD NewCol INT

GO

UPDATE [MyTable] 
    SET [NewCol] = 1

The batch separator GO is not supported, and without it the second statement fails.

Are there any solutions to this other than using multiple command executions?

Answer

JotaBe picture JotaBe · Sep 5, 2014

The GO keyword is not T-SQL, but a SQL Server Management Studio artifact that allows you to separate the execution of a script file in multiple batches.I.e. when you run a T-SQL script file in SSMS, the statements are run in batches separated by the GO keyword. More details can be found here: https://msdn.microsoft.com/en-us/library/ms188037.aspx

If you read that, you'll see that sqlcmd and osql do also support GO.

SQL Server doesn't understand the GO keyword. So if you need an equivalent, you need to separate and run the batches individually on your own.