You can step through the code and check the value of
s
before it executes.
Building and Debugging (Visual C#)[
^]
This is VERY useful for debugging.
Ok. So I'll give you a hint.
There are two way to end a statement in T-SQl. The keyword
GO
and the semi colon
;
. Their use is slightly different, but that's not important here.
So you have
s1
and
s2
which are in the syntax "[some SQL];". I'm sure you tested these but note the semi colon on the end.
then you have
s
which is "[some SQL] union s1; union s2;"
Do you see it?
This is what SQL sees:
[START QUERY]
[some SQL] union [some SQL]
[END QUERY] <= the semi colon
[START QUERY]
union s2;
[SYNTAX ERROR NEAR UNION]
When you run a single query at a time, you don't even need a semi colon. The statement will terminate if there is nothing left to do. You would only add it in a query if you wanted to perform more that one action.
Just remove the semi colon