Creating Stored Procedure Syntax, relating to use of GO

sql, sql-server, sql-server-2005, stored-procedures

Solution

There was a bug released in SQL Server that parses the GO statement incorrectly. I believe it was introduced when you could do GO X, and execute the batch X multiple times.

Sometimes I've had to add a comments section ("--") to force the parser to terminate rather than produce a syntax error. This has been seen when I've had 400,000 lines in a batch of code.

Example:

PRINT "This is a test."

GO --

PRINT "This is not a test."

GO 5 --

Problem

Does anyone know why. ``` CREATE PROCEDURE My_Procedure (@Company varchar(50)) AS SELECT PRD_DATE FROM WM_PROPERTY_DATES WITH (NOLOCK) WHERE PRD_COMPANY = @Company GO ``` Gives me an error message in SQL management studio: `Msg 102, Level 15, State 1, Procedure My_Procedure, Line 1 Incorrect syntax near 'GO'.` Now, this is the last statement of a batch, maybe the last statement should not have a `GO` ?

Original source