了解T-SQL GOTO [英] Understanding T-SQL GOTO

查看:107
本文介绍了了解T-SQL GOTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的老师最近谈到了要在Management Studio中使用的语句"GOTO",但起初我不太理解.他说,在我可以自己命名的标签的帮助下,GOTO被用于跳转到不同的代码段.这是他用来表示示例的代码:

My teacher recently talked about the statement 'GOTO', to be used in Management Studio, but I didn't quite understand it at first. He stated that GOTO is being used to jump to different pieces of code, with the help of a label I can name by myself. This is the code he used to represent his example:

select 'first'

goto jump
select 'second'

jump:
select 'third'

当我执行代码时,果然,它会打印第一"和第三".现在我的问题是,首先选择的第二个"是做什么的?

When I execute the code, sure enough, it prints 'first' and 'third'. My question now is what was the select 'second' doing there in the first place?

推荐答案

在您的示例中,第二个select显然是不必要的.

In your example, the second select is clearly unnecessary.

GOTO是程序代码的控制流"示例.它是最早的计算机语言的一种构造,可以直接映射到处理诸如C或汇编代码之类的语言时硬件的工作方式.从那时起,它已被包含在许多其他语言中.

GOTO is an example of "control-flow" for the the program code. It is a construct from the earliest computer languages, one that maps directly onto how the hardware works when processing languages such as C or assembly code. Since then, it has been included in many other languages.

GOTO通常与IF一起使用.但是,T-SQL提供了更好的控制流功能,例如:

GOTO would often be used with IF. However, T-SQL offers better control flow functionality, such as:

  • 何时
  • IF/开始
  • 尝试/抓住

通常,您应该使用这些结构,而不要使用GOTO.实际上,GOTO颇具争议.许多人认为它始终是不良代码的标志(有时用意大利面条式代码"来描述这种类型的代码).其他人将为异常处理(我有时这样做)或某些类型的状态机之类的异常异常.

In general, you should be using these constructs and not GOTO. In fact, GOTO is rather controversial. Many people think it is always a sign of poor code ("spaghetti code" is sometimes used to describe this type of code). Others will make a very rare exception for something like exception handling (which I sometimes do) or some types of state machines.

我认为,GOTO仅应在所有其他构造之后再讲授,并且仅用于非常特定的目的.

In my opinion, GOTO should only be taught after all the other constructs and only for very specific purposes.

这篇关于了解T-SQL GOTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆