为什么不应该将 Transact-SQL 语句组合在同一个批处理中?

2021-09-10 00:00:00 tsql sql-server technet

TechNet 的 BEGIN...END 描述让我对以下评论感到困惑:

The BEGIN...END description of TechNet make me confused with following remark:

尽管所有 Transact-SQL 语句在 BEGIN...END 块中都有效,但某些 Transact-SQL 语句不应组合在同一个批处理(语句块)中.

Although all Transact-SQL statements are valid within a BEGIN...END block, certain Transact-SQL statements should not be grouped together within the same batch (statement block).

https://technet.microsoft.com/en-us/library/aa225998(v=sql.80).aspx

谁能告诉我为什么不应该将它们分组在 BEGIN ... END 块中?当我使用 BEGIN...END 创建区域时是否有任何问题作为这个建议:sql server #region?

Could anybody tell me why they shouldn't be grouped within a BEGIN ... END block? Is there any problem when I use BEGIN...END to create region as this suggestion: sql server #region?

推荐答案

如果需要在 T-SQL 脚本中创建多个批处理,则需要使用 GO 语句明确分隔命令组.因此,即使您没有将 BEGIN...END 块(批处理)显式添加到代码中,它也会隐式创建.因此,明确添加它不应该给您带来任何额外的麻烦.

If you need to create multiple batches in T-SQL script, you need to separate the group of commands explicitly with the GO statement. So BEGIN...END block (batch) is created implicitly even if you do not add it explicitly to your code. So adding it explicitly shoud not create any additional troubles for you.

某些命令不能按照链接的 MSDN 文章:

创建默认值、创建函数、创建过程、创建规则、创建TRIGGER 和 CREATE VIEW 语句不能与其他批处理语句.CREATE 语句必须开始批处理.全部该批次中的其他语句将被解释为一部分第一个 CREATE 语句的定义.

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.

如果 EXECUTE 语句是批处理中的第一条语句,则 EXECUTE关键字不是必需的.EXECUTE 关键字是必需的,如果EXECUTE 语句不是批处理中的第一条语句.

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.

相关文章