SQL Error (102): Incorrect syntax near “GO”
DON’T USE NVARCHAR FOR SQL VARIABLES HOLDING BATCHED SQL STATEMENTS WITH IMBEDDED “GO” STATEMENTS…
I recently wrote some code to generate CRUD procedures for a Data Warehouse project and I was receiving an error message on the batch T-SQL “GO” statement when executing the sql code that was generated ?
Msg 102, Level 15, State 1, Procedure, Line 1
Incorrect syntax near ‘GO’.
When I looked at the generated sql output I could not see any issues syntactically??? I beat my head against the wall reviewing the code for about an hour trying to figure out why it would not run. The only thing that I saw was that I was using the “+ char(13)” line feed in the dynamic sql script generation. Out of pure frustration I decided to wrap the “GO” statements in a CRLF or CHAR(13) + CHAR(10). Lo and behold the batched create procedure statements ran without issue or so it seemed. I was still getting intermittent issues with the “GO” statements . I then checked my SSMS query settings for the batch separator and it was set to “GO”. I decided to take another look at the code itself and I noticed I had set the variable holding the actual SQL statements to NVARCHAR(MAX). I thought to myself could this be a contributing factor? I guess it could allow for non printable characters. I changed any occurance of any variable that was used to hold any sql code VARCHAR and the “GO” statements ran without issue. This is the first time I have encountered this issue maybe it has something to do with SSMS 2012? In any case if you encounter something similar maybe this post will help.