GO
is actually not valid T-SQL:
GO is not a Transact-SQL statement; it is a command recognized by the
sqlcmd and osql utilities and SQL Server Management Studio Code
editor.
You will have to remove instances of GO
in your dynamic SQL, or use one of the tools mentioned on the article (such as osql
from the command-line). Your query should still work with all instances of GO
removed from the dynamic SQL.