2

I don't understand what's happening.

This generates an error

create procedure sp_test
as
/*
   /*
      a
   */
   e'
*/
begin
print''
end
go

"Msg 102, Level 15, State 1, Procedure sp_test, Line 13 Incorrect syntax near 'go'."

While this works

create procedure sp_test
as
/*
   /*
      a
   */
   e
*/
begin
print''
end
go

Why if I have two nested comments in the main comment I can't have ' symbol? I discovered this bug using VS Sql compare to generate the db script and is not possible to have any other GO after this.

Instead using Sql Management it will generate the single sp_test script without GO..

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
elle0087
  • 840
  • 9
  • 23
  • 1
    This looks like a bug in management studio, the `GO` "statement" is not a real statement but used by management studio and other programs to separate a big sql query into smaller pieces, executed one by one. It looks like management studio is tripped up by the nested comments. The editor shows coloring just fine so it must be the part of the tool that tries to figure out where to split it that doesn't support nested comments properly. – Lasse V. Karlsen Sep 09 '16 at 08:34
  • yes, i agree you, it s a annoying problem that it s stopping me to work...and i don t know how to turn around it.. – elle0087 Sep 09 '16 at 08:41
  • If you want I can report it using Microsoft Connect. Or you can do it yourself. – Lasse V. Karlsen Sep 09 '16 at 08:43
  • @LasseV.Karlsen and user3061212: I reported this bug back in March -- ["GO" in 2nd half of nested block comments breaks batch parsing in SSMS and SQLCMD](https://connect.microsoft.com/SQLServer/feedback/details/2488045/go-in-2nd-half-of-nested-block-comments-breaks-batch-parsing-in-ssms-and-sqlcmd) -- but I don't believe they will fix it. However, there is a work-around as I show in my [answer below](http://stackoverflow.com/a/39416258/577765). – Solomon Rutzky Sep 09 '16 at 16:50

2 Answers2

1

This must be a bug in SQL Server Management Studio.

The GO statement is not a real statement that SQL Server knows how to handle but a convention that editors, such as Management Studio and the command line client, uses to delimit big queries into smaller pieces.

These smaller pieces are then executed one by one in order.

As such, if the GO command is actually sent to SQL Server to execute, it won't know how to handle it and thus gives you the error you got.

In Management Studio 2014, the syntax coloring is fine with the nested comments, but the presence of the apostrophe inside trips up the code that tries to delimit the query into smaller pieces.

As such I think the bug here is that the code that tries to split on the GO statement does not in fact support nested comments and thus is tripped up by the presence of them. Basically it seems to think that the comment ends after the inner */, which is wrong, and then the apostrophe is considered the start of a string that has no end that then encapsulates everything that follows, including the GO command.

Thus everything after the apostrophe is sent to SQL Server. SQL Server does support nested comments so it will see the GO command as a statement, which it doesn't support, and thus the error.


I have reported this using Microsoft Connect here: SQL Server 2014 Management Studio, when delimiting on GO command, doesn't handle nested comments.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • the problem is that when you compile the sp with nested comments and ' you haven t any problems, and it s correct because the ' are in the comments, where i can edit everythings! but when you have to compile more sp you can t because...i dont know it! – elle0087 Sep 09 '16 at 08:45
  • To be honest this is unlikely to be a problem for very many so priority-wise I wouldn't expect people at Microsoft to rush to fix this, simply because nested comments are very seldom used. Why do you need to use nested comments? As an interim fix, simply replace the start and end of the inner comment(s) with `* / ... * /` (space between) so that the `GO` "parser" isn't tripped up. – Lasse V. Karlsen Sep 09 '16 at 08:47
  • yes i know is not normal, but it s possible...unfortunately in Italy use lot of '...can you report this bug to Microsoft? thank you! – elle0087 Sep 09 '16 at 08:52
  • I will report it and post a link in my answer. – Lasse V. Karlsen Sep 09 '16 at 08:52
0

While SQL Server itself allows for nested block comments, the batch parsing code used by SSMS, SQLCMD.EXE, and possibly SMO, has a bug in it that doesn't handle these nested block comments entirely. I emphasize "entirely" because that code will handle them to a degree. It all depends on how they are nested and where the embedded GO or apostrophe, etc are. Some combinations work while others do not. For example, the following adaptation of your non-working test does work because I added a 2nd nested comment:

create procedure sp_test
as
/*
   /*
      a
   */
   /*
   e'
   */
*/
begin
print''
end
go

I posted this bug to Microsoft Connect back in March, but I doubt it will ever be seen as having enough priority to devote developers to, sadly:

"GO" in 2nd half of nested block comments breaks batch parsing in SSMS and SQLCMD

P.S. It should be noted that the older OSQL.EXE does not seem to have this particular parsing problem, though I still would not recommend using it ;-).

Solomon Rutzky
  • 46,688
  • 9
  • 128
  • 171