简体   繁体   English

在什么情况下可以接受使用T-SQL GOTO语句?

[英]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. 在关于Microsoft T-SQL代码格式的“宗教”讨论中,我质疑GOTO语句是否仍然以T-SQL语法提供。 In 13 years of using T-SQL I have never had occasion to use it, and indeed didn't know if it existed. 在使用T-SQL的13年中,我从来没有机会使用它,并且确实不知道它是否存在。 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? 至少一个情况下 GOTO语句将产生执行总比一个人在使用其他高阶编程结构的解决方案?

  • 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? 如何在不复制/粘贴的情况下复制错误处理功能?

I almost never use GOTO and can easily live without it. 我几乎从不使用GOTO ,没有它就可以轻松生活。

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. 我可能想在错误返回时采取一些操作, GOTO允许我为错误检查定义一个代码块。

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 . . . 您可以通过多种方式解决此问题,但GOTO是一个合理的选项,可以保证错误得到一致处理,并且代码不会被大量的if @Error = 0 . . .混乱if @Error = 0 . . . if @Error = 0 . . . statements. 声明。

i saw goto a few times in large scripts where people used it to improve readability. 我在大型脚本中看到了几次goto,人们用它来提高可读性。 sometimes it is better readable but mostly it turns into spaghetti code. 有时它更易读,但大多数情况下变成了意大利面条代码。

i see just one situation where goto can maybe perform better. 我只看到goto可能表现更好的一种情况。 its inside a multiple while loop. 它在多个while循环中。 you can use goto once instead of multiple breaks which just exists the innermost loop. 你可以使用goto一次而不是多次中断 ,它只存在于最里面的循环中。 nevertheless in my opinion break is not much better than goto, its both not a good programming style. 尽管如此,在我看来,休息并不比goto好多少,它的编程风格都不是很好。

while ...
  while ...
    while... 
    break
  break
break


while ...
  while ...
    while... 
    goto endloops

endloops:

I've used GOTO statements mainly when SSIS was not available (don't ask!!) to provide some control flow to a large stored procedure used for ETL processing. 我主要在SSIS不可用时使用GOTO语句(不要问!!)为用于ETL处理的大型存储过程提供一些控制流。 That's the only time I've ever used them and while useful in that scenario, it's obviously not a scenario you'd be in. 这是我唯一一次使用它们,虽然在这种情况下很有用,但它显然不是你所处的场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM