views:

1022

answers:

6

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 ?

A: 

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.

Dave Markle
Thanks, adding a semi-colon has no impact, same error message results. Its definitely a funny little problem.
Mike D
A: 

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.

Remus Rusanu
Thanks, bit bitchy, but thanks. I can explain, we want each statement in DDL terms to operate like a batch, any errors coming back and logged and reported. I appreciate your response, but not sure what you're talking about, as a set of 50 ddl statements, say 50 with a GO after each one, all usually work fine, its something to do with this last statement, about the procedure creation thats causing the issue i think.
Mike D
And, you should have said 'contend' not 'pretend', surely. I'm fairly sure we don't 'pretend' to add GO after each statement.
Mike D
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.
Remus Rusanu
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.
Mike D
Remus Rusanu
Hi Remus, I'm not using a file, I'm using the offical, SQL Management Studio tool, and there is definetly a carriage return.
Mike D
I mean the SQL file you are running in SSMS. A 'soft' carriage return (\r) would look the same as a 'hard' carriage return (\r\n) in SSMS, you need a hex editor to validate this. If this is a one time incident then you can simply delete the @Company\rGO\r\n and type again the whole deal, just to make sure is right. Most signs of you error point toward a problem with the carriage return between @Company and GO.
Remus Rusanu
+1  A: 

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.

Rune Sundling
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
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
Rune Sundling
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.
Mike D
A: 

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

Raj
Thanks Raj, tried the BEGIN and END, but same result.
Mike D
+1  A: 

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.

Paul Rowland
A: 

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

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