Thought I’d document this little gem today. I do not recall when I first came across this particular issue, but it sometimes rears its ugly head and can be quite the bugger to figure out.
To set the stage, imagine that the queries used in these examples are actually hundreds or thousands of lines long. The script itself is a manual maintenance script saved on a shared drive that a group of dev/admins use for various tasks.
Commented out sections could actually contain simple comments, old queries, update, delete, insert, or even truncate statements.
Let’s start with a simple query and execute each in turn:
SELECT 'something'
;
GO
Now, comment it out with the multi-line comment and add a new query.
/*
SELECT 'something'
;
GO
*/
SELECT 'different'
;
GO
Now this is where we start getting to have fun. Let’s say that this script is several hundred or thousands of lines long and you need to remove a large section from being executed so, you decide to wrap the entire top section with a multi-line comment:
/*
/*
SELECT 'something'
;
GO
*/
SELECT 'different'
;
GO
*/
SELECT 'BOOM'
;
GO
From the best of my experimentation the issue lies in having a nested multi-line comment and the outmost comment block contains the keyword “GO”. (Or whatever value the Query Execution Batch separator is set to.)
Your choice on how to best resolve this issue. A couple of quick methods to resolve:
1) Find and replace the GO keyword with an empty string
2) Single-line comment the offending GO statement or the entire comment section(s):
--/*
--/*
--SELECT 'something'
--;
--GO
--*/
--SELECT 'different'
--;
--GO
--*/
SELECT 'BOOM'
;
GO
As fun: this actually works. Though not be a practical solution if you cannot quickly identify where the offending GO statement is actually located.
/*
/*
SELECT 'something'
;
GO
SELECT 'different'
;
GO
*/*/
SELECT 'BOOM'
;
GO
And this one breaks it again:
/*
/*
SELECT 'something'
;
GO
SELECT 'different'
;
GO
*/*/
/*
/* */
GO
*/
SELECT 'BOOM'
;
GO
Have a little fun and experiment with this one if you’d like.
Leave a comment