0

I am writing a script that needs to do something like this:

IF [CONDITION]

BEGIN

--EXECUTE LOADS OF BATCHES (I.E. BLOCKS OF CODE WITH 'GO' AT THE END)

END

This appears not to be allowed. The GO statement is not allowed in a BEGIN...END block.

I've also tried this:

IF NOT [CONDITION] GOTO GetMeOutOfHere

--EXECUTE LOADS OF BATCHES (I.E. BLOCKS OF CODE WITH 'GO' AT THE END)

GetMeOutOfHere:

But, you guessed it, GOTO doesn't work across batches.

Is there any solution to this confounded conundrum?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
David
  • 15,750
  • 22
  • 90
  • 150

2 Answers2

1

Just get rid of the GO statements, they aren't needed as long as you aren't doing structure modifications.

If you are changing the structure, you may need to duplicate your if conditions.

cjk
  • 45,739
  • 9
  • 81
  • 112
0

What about this:

IF NOT [CONDITION]

SET NOEXEC ON

--EXECUTE LOADS OF BATCHES (I.E. BLOCKS OF CODE WITH 'GO' AT THE END)

SET NOEXEC OFF

It does seem to work with the script I'm doing right now.

David
  • 15,750
  • 22
  • 90
  • 150
  • Well if that's what you want to do. See [this question and answers](http://stackoverflow.com/q/659188/73226) for other possible methods. – Martin Smith Sep 20 '11 at 12:06
  • It is what I want to do. The `RAISERROR` method would terminate the script entirely, whereas with `SET NOEXEC ON / OFF` I can determine a point to continue from. – David Sep 20 '11 at 14:55