Fun with Commenting codes in SQL Server – Programming Code Examples
SQL Server

Fun with Commenting codes in SQL Server4 min read

If a working code is interesting then a code that doesn’t work is much fascinating .

I have got one question . Where does commenting using – – or /* */ doesn’t work in SQL server?

It is pretty good question to think .There are certain places where commenting won’t work in SQL server. And those are the ones that i will be mentioning in this post . The situation where i found this was through a mistake . I was working on a procedure and wanted to execute it in a test name instead of it’s original name . Example – I was working under a procedure called ‘NewStudentsList’ .Now i wanted the original procedure to be untouched and so i commented the name of the original procedure and wanted to give it a name like ‘TestNewStudentsList’ and i ended up here by mistake.




Below mentioned is the example of my procedure .

Now instead of commenting the whole name ,i commented it inside the square brackets .

Usually SSMS changes the commented code in green color .But in the above image one can clearly see that the code is not commented .

Another interesting fact is the comment won’t even work until the open bracket’s scope is closed with a closed bracket. Usually line comments work in the next line .In the below image i have opened a bracket and its scope is active till it is closed.Once the close bracket is applied then the commenting can be done as usual.

Also i tried with various other symbols can found many places it works and doesn’t work.The below screenshot is the symbols where commenting works .

Now lets see few places that commenting with symbols won’t work.

But in the first two case you can use this small trick and make it work . Using — three lines instead of two will make things work .

But in the below two cases the commenting won’t work until the scope is closed .This doesn’t support both single and block commenting .

As mentioned earlier commenting won’t work inside single quote as we know it is considered as string .

This is quite a different post to think about .Kindly do comment on this post if you people have a reason for this or solution . Also Let’s stay home quarantined during this pandemic and save the lives of our fellow ones . Stay Home Stay Safe !!!

Leave a Comment