5

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 ?

1
  • If I don't use the GO, then it works fine. But our company standard is to add GO after each statement. Commented Jun 25, 2009 at 11:44

10 Answers 10

12

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

Sign up to request clarification or add additional context in comments.

1 Comment

This was the right answer for me! Not sure why the "GO X" answer above is the accepted answer since the OP SQL has no "GO X" in it.
4

The sql you currently have in the question will work properly. The unformatted sql you had before Kev edited the post won't. The reason is that you had the GO on the same line as the sql. It needs to be on a separate line.

3 Comments

Thanks dude, but, its just the way its formatted on this site, GO is on its own line. Formatting on site now looks fine btw.
I know it's formatted this way here now. But it wasn't when you initially posted it. Then you had the entire sql on one line, which is the same that ran in SSMS, and that's why you get the "Line 1 Incorrect syntax". The go needs to be on a separate line
interesting point, thanks. You can see my problem tho, I have you guys saying, GO must be on a new line, i am using the return key, to put it on a new line, but the problem persists. Tho, i'll look at your last comment more closely and experiment.
3

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

2 Comments

Just for others following -- it's not clear to me why this is the accepted answer :) The error message points at ".. line 1 .." when there are clearly more lines than 1. That suggests either reformatting was the answer (putting GO on a line on its own) or solving a line feed problem (as in some answers below). If the answer here doesn't work, take a look downstairs.
And check out the comment below ... "Thanks dude, but, its just the way its formatted on this site, GO is on its own line. Formatting on site now looks fine btw. – Mike D Jun 25 '09 at 11:56"
3

If you copy-paste text from text editor with Unix/Mac EOLs (e.g. Notepad++ supports this) the GO is interpreted as being on the same line as the last TSQL statement (yet on the screen you can see newlines normally). Converting EOLs to Windows (CRLF) in the text editor fixed the problem. Very tricky though.

1 Comment

I just had the same problem after copying a procedure creation script from a .sql file... thanks fot the explanatio, it was driving me crazy!
1

Error for this sql

ALTER PROCEDURE My_Procedure
    (@Company varchar(50))  
AS  
    SELECT PRD_DATE 
    FROM WM_PROPERTY_DATES 
    WITH (NOLOCK) 
    WHERE PRD_COMPANY = @Company GO

is

Msg 102, Level 15, State 1, Procedure My_Procedure, Line 7
Incorrect syntax near 'GO'.

note the Line 7, original question has Line 1.

If I put the GO on its own line SQL works fine.

Given that your error message says Line 1, it would appear that for some reason there isnt a correct CR/LF happening in your sql.

Comments

0

You can certainly have GO at the end of your batch. I see nothing wrong with this code per se. Put a semicolon after @Company.

1 Comment

Thanks, adding a semi-colon has no impact, same error message results. Its definitely a funny little problem.
0

I tried this SQL on my 2008 server by creating a table WM_PROPERTY_DATES and adding 2 columns, PRD_DATE and PRD_COMPANY.

Work just fine and creates the proc. Maybe you can try putting your code in a BEGIN...END block and see if the issue persists.

Raj

Comments

0

You said

Now, this is the last statement of a batch, maybe the last statement should not have a GO ?

This implies that these lines are all part of the same batch submitted to SQL. The thing is, a CREATE PROCEDURE (or CREATE FUNCTION or CREATE VIEW) statement must be the first statement in the batch. So, put a "GO" line in front of that CREATE statement, and see what happens.

Philip

1 Comment

SQL Management studio replies with: "A fatal scripting error occurred. Incorrect syntax was encountered while parsing GO."
0

In my case I had copied part of the code from a webpage and it seems that saved the page with different encoding, I tried SaveAs from SMS with different encoding, but didn't work.

To fix my issue I copy the code into NodePad, then save it in ANSI format and re-open the query

Comments

-1

No serious company could possibly pretend to add GO after each statement. Perhaps after each batch.

GO is not a Transact-SQL statement. Is a delimiter understood by tools like ISQLW (aka. Query analizer), osql, sqlcmd and SSMS (Management Studio). These tools split the SQL file into batches, delimited by GO (or whatever is the 'batch delimiter' set, to to be accurate, but is usually GO) and then send to the server one batch at a time. The server never sees the GO, and if it would see it then it would report an error 102, incorrect syntax, as you already seen.

6 Comments

And, you should have said 'contend' not 'pretend', surely. I'm fairly sure we don't 'pretend' to add GO after each statement.
Bitchy sometimes helps ;) If you find that the LAST DDL has a problem with the GO the likely problem is that it does not have a CR/LF after it: the file ends with GO<EOF>, not with GO\r\n<EOF>. Some tools have problem with that.
Cheers bud, in this example, I'm running the SQL directly in SQL Management Studio (2005). Either adding an explicit carriage return or not having a carriage return - has no impact on the problem. Replace the table I have used, with any table, its a reproducible issue.
Perhaps is a CR only in your file, not a CR/LF: connect.microsoft.com/SQLServer/feedback/…
Hi Remus, I'm not using a file, I'm using the offical, SQL Management Studio tool, and there is definetly a carriage return.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.