Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Stored Procedure Syntax, relating to use of GO

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 ?

like image 337
Mike D Avatar asked Jun 25 '09 11:06

Mike D


2 Answers

If you go to "Save As...", click on the little down-arrow on the Save button and select "Save with Encoding..." then you can set your Line endings to Windows (CR LF). That seems to have resolved the issue for me...

like image 52
utexaspunk Avatar answered Nov 11 '22 02:11

utexaspunk


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 --

like image 23
Dustin Buschow Avatar answered Nov 11 '22 00:11

Dustin Buschow