While researching something I can across an MSDN article which says:
在研究我可以在MSDN文章中做的事情时说:
Exits unconditionally from a query or procedure. RETURN is immediate and complete and can be used at any point to exit from a procedure, batch, or statement block. Statements that follow RETURN are not executed.
从查询或过程无条件退出。 RETURN是立即完成的,可以在任何时候用于退出过程,批处理或语句块。不执行RETURN后面的语句。
The documentation didn't come close to the specific situation I was researching. After reading it I realized that I didn't understand the Return statement, and (more specifically) the definition of "statement blocks" as well as I thought. So...
文档没有接近我正在研究的具体情况。阅读之后,我意识到我不理解Return语句,并且(更具体地说)“语句块”的定义以及我认为。所以...
What exactly is a t-sql "statement block"? Are they defined by Begin ... End
like brackets in C#, { ... }
, or something else entirely?
究竟什么是t-sql“语句块”?它们是否由Begin ... End定义为C#中的括号,{...}或完全不同的东西?
Thanks!
2 个解决方案
#1
5
They come in a variety of flavors (try, catch) but generally they look like
它们有各种各样的口味(尝试,捕捉)但通常看起来像
BEGIN
PRINT 'I am a block'
RETURN
PRINT 'I am still in a block but you will not see me'
END
PRINT 'Too late, we returned from the above block'
#2
1
I would agree that that sentence in the documentation is misleading.
我同意文件中的这句话具有误导性。
RETURN
will not just exit the most immediate BEGIN END
block (which can be nested), but will return from the entire function or procedure, and I'm not sure what it really means to exit a batch - perhaps in SSMS with the GO separator, it would continue to run later batches (I'll have to test that) - GO is a client side thing, so I'm not sure how useful that concept is in practice.
RETURN不仅会退出最直接的BEGIN END块(可以嵌套),而是从整个函数或过程返回,我不确定退出批处理的真正含义 - 也许是在带有GO分隔符的SSMS中,它将继续运行以后的批次(我将不得不测试) - GO是客户端的事情,所以我不确定这个概念在实践中有多么有用。
#1
5
They come in a variety of flavors (try, catch) but generally they look like
它们有各种各样的口味(尝试,捕捉)但通常看起来像
BEGIN
PRINT 'I am a block'
RETURN
PRINT 'I am still in a block but you will not see me'
END
PRINT 'Too late, we returned from the above block'
#2
1
I would agree that that sentence in the documentation is misleading.
我同意文件中的这句话具有误导性。
RETURN
will not just exit the most immediate BEGIN END
block (which can be nested), but will return from the entire function or procedure, and I'm not sure what it really means to exit a batch - perhaps in SSMS with the GO separator, it would continue to run later batches (I'll have to test that) - GO is a client side thing, so I'm not sure how useful that concept is in practice.
RETURN不仅会退出最直接的BEGIN END块(可以嵌套),而是从整个函数或过程返回,我不确定退出批处理的真正含义 - 也许是在带有GO分隔符的SSMS中,它将继续运行以后的批次(我将不得不测试) - GO是客户端的事情,所以我不确定这个概念在实践中有多么有用。