为什么不应该将 Transact-SQL 语句组合在同一批中?
Why shouldn't Transact-SQL statements be grouped together within the same batch?
TechNet 的 BEGIN...END 描述让我对以下评论感到困惑:
虽然所有 Transact-SQL 语句在 BEGIN...END 块中都是有效的,但某些 Transact-SQL 语句不应在同一批(语句块)中组合在一起。
https://technet.microsoft.com/en-us/library/aa225998(v=sql.80).aspx
有人能告诉我为什么不应该将它们分组在 BEGIN ... END 块中吗?当我按照这个建议使用 BEGIN...END 创建区域时有什么问题吗:sql server #region?
BEGIN..END 不是 视觉选项 为了能够折叠区域,您实际上是在告诉这些串线在一起的服务器。将它用作 #region 只是一个 解决方法 。
这是因为 BEGIN 和 END 之间的任何内容都将作为批处理执行,并且批处理的所有规则都适用于它。
想一想当其中一个语句出错时会发生什么,你想让其他语句继续吗?
在此处查看更多内容:Batches
您可以使用评论来区分不同的批次,如下所示。
BEGIN /** Region1 Starts**/
....
....
....
END /** Region1 ends**/
BEGIN /** Region2 Starts **/
....
....
....
END /** Region2 Ends **/
如果需要在 T-SQL 脚本中创建多个批处理,则需要使用 GO
语句显式分隔命令组。所以 BEGIN...END 块(批处理)是隐式创建的,即使您没有将它显式添加到代码中也是如此。所以明确地添加它应该不会给你带来任何额外的麻烦。
中所指定,某些命令不能在一批中组合在一起
CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE
TRIGGER, and CREATE VIEW statements cannot be combined with other
statements in a batch. The CREATE statement must begin the batch. All
other statements that follow in that batch will be interpreted as part
of the definition of the first CREATE statement.
A table cannot be altered and then the new columns referenced in the
same batch.
If an EXECUTE statement is the first statement in a batch, the EXECUTE
keyword is not required. The EXECUTE keyword is required if the
EXECUTE statement is not the first statement in the batch.
TechNet 的 BEGIN...END 描述让我对以下评论感到困惑:
虽然所有 Transact-SQL 语句在 BEGIN...END 块中都是有效的,但某些 Transact-SQL 语句不应在同一批(语句块)中组合在一起。
https://technet.microsoft.com/en-us/library/aa225998(v=sql.80).aspx
有人能告诉我为什么不应该将它们分组在 BEGIN ... END 块中吗?当我按照这个建议使用 BEGIN...END 创建区域时有什么问题吗:sql server #region?
BEGIN..END 不是 视觉选项 为了能够折叠区域,您实际上是在告诉这些串线在一起的服务器。将它用作 #region 只是一个 解决方法 。
这是因为 BEGIN 和 END 之间的任何内容都将作为批处理执行,并且批处理的所有规则都适用于它。
想一想当其中一个语句出错时会发生什么,你想让其他语句继续吗?
在此处查看更多内容:Batches
您可以使用评论来区分不同的批次,如下所示。
BEGIN /** Region1 Starts**/
....
....
....
END /** Region1 ends**/
BEGIN /** Region2 Starts **/
....
....
....
END /** Region2 Ends **/
如果需要在 T-SQL 脚本中创建多个批处理,则需要使用 GO
语句显式分隔命令组。所以 BEGIN...END 块(批处理)是隐式创建的,即使您没有将它显式添加到代码中也是如此。所以明确地添加它应该不会给你带来任何额外的麻烦。
CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE TRIGGER, and CREATE VIEW statements cannot be combined with other statements in a batch. The CREATE statement must begin the batch. All other statements that follow in that batch will be interpreted as part of the definition of the first CREATE statement.
A table cannot be altered and then the new columns referenced in the same batch.
If an EXECUTE statement is the first statement in a batch, the EXECUTE keyword is not required. The EXECUTE keyword is required if the EXECUTE statement is not the first statement in the batch.