Search code examples
sqlt-sqlcoding-stylegoto

In what case would the use of a T-SQL GOTO statement be acceptable?


In a "religious" discussion about formatting of Microsoft T-SQL code, I questioned whether or not the GOTO statement was still available in T-SQL syntax. In 13 years of using T-SQL I have never had occasion to use it, and indeed didn't know if it existed. After a brief search of the documentation and to my consternation it does indeed exist!

My question is this:

Is there at least one case where GOTO statements would yield a solution that performs better than one in which other higher order programming constructs are used?

  • Encryption algorithm implementation

My question is NOT:

  • How do I use small bits of functionality without creating a whole function to do so?
  • How do I replicate error handling functionality without copy/paste?

Solution

  • I almost never use GOTO and can easily live without it.

    The one case where I would consider using it is when I have complicated code that does lots of error checking. I might want to take some action when an error returns, and GOTO allows me to define a single block of code for the error checking.

    You can solve this problem in multiple ways, but GOTO is a reasonable option that guarantees that errors are processed consistently and the code is not cluttered with a lot of if @Error = 0 . . . statements.