在 SQL Server 中,什么时候应该使用 GO,什么时候应该使用分号 ;?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3701147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:32:15  来源:igfitidea点击:

In SQL Server, when should you use GO and when should you use semi-colon ;?

sqlsql-server

提问by HAdes

I've always been confused with when I should use the GO keyword after commands and whether a semi-colon is required at the end of commands. What is the differences and why/when I should use them?

我一直对何时应该在命令后使用 GO 关键字以及命令末尾是否需要分号感到困惑。有什么区别,为什么/什么时候我应该使用它们?

When I run the Generate-script in SQL Server Management Studio, it seems to use GO all over the place, but not the semi-colon.

当我在 SQL Server Management Studio 中运行 Generate-script 时,它似乎到处都在使用 GO,但没有使用分号。

采纳答案by cjk

GOonly relates to SSMS - it isn't actual Transact SQL, it just tells SSMS to send the SQL statements between each GOin individual batches sequentially.

GO仅与 SSMS 相关 - 它不是实际的 Transact SQL,它只是告诉 SSMS 在每个GO批次之间按顺序发送 SQL 语句。

The ;is a SQL statement delimiter, but for the most part the engine can interpret where your statements are broken up.

;是一个 SQL 语句分隔符,但在大多数情况下,引擎可以解释您的语句被分解的位置。

The main exception, and place where the ;is used most often is before a Common Table Expression Statement.

主要的例外和;最常使用的地方是在公共表表达式语句之前。

回答by Conrad Frix

The reason why you see so many GO's in Generated DDL scripts is because of the following ruleabout batches.

在 Generated DDL 脚本中看到这么多 GO 的原因是因为以下关于批处理的规则

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.

CREATE DEFAULT、CREATE FUNCTION、CREATE PROCEDURE、CREATE RULE、CREATE TRIGGER 和 CREATE VIEW 语句不能与批处理中的其他语句组合。CREATE 语句必须开始批处理。该批处理中的所有其他语句将被解释为第一个 CREATE 语句定义的一部分。

One of the use cases for Generated DDL is to generate multiple objects in a single file. Because of this a DDL generator must be able to generate batches. As others have said the GO statement ends the batch.

Generated DDL 的用例之一是在单个文件中生成多个对象。因此,DDL 生成器必须能够生成批处理。正如其他人所说,GO 语句结束批处理。

回答by Raj More

GO

Go is a batch separator. This means that everything in that batch is local to that particular batch.

Go 是一个批处理分隔符。这意味着该批次中的所有内容对于该特定批次都是本地的。

Any declarations of Variables, Table Variables, etc do not go across GOstatements.

变量、表变量等的任何声明都不会跨越GO语句。

#Temp tables are local to a connection, so they span across GO statements.

#Temp 表是连接本地的,因此它们跨越 GO 语句。

Semicolon

分号

A Semicolon is a statement terminator. This is purely used to identify that a particular statement has ended.

分号是语句终止符。这纯粹用于标识特定语句已结束。

In most cases, the statement syntax itself is enough to determine the end of a statement.

在大多数情况下,语句语法本身足以确定语句的结尾。

CTE's however, demand that the WITH is the first statement so you need a semicolon before the WITH.

但是,CTE 要求 WITH 是第一个语句,因此在 WITH 之前需要一个分号。

回答by onedaywhen

You should use a semi-colon to terminate every SQL statement. This is defined in the SQL Standards,

您应该使用分号来终止每个 SQL 语句。这是在 SQL 标准中定义的,

Sure, more often than not SQL Server allows you to omit the statement terminator but why get into bad habits?

当然,SQL Server 通常允许您省略语句终止符,但为什么要养成坏习惯呢?

As others have pointed out, the statement preceding a common table expression (CTE) must be terminated with a semi-colon. As a consequence, from folk who have not fully embraced the semi-colon terminator, we see this:

正如其他人指出的那样,公共表表达式 (CTE) 之前的语句必须以分号结尾。因此,从还没有完全接受分号终止符的人那里,我们看到了这一点:

;WITH ...

which I think looks really odd. I suppose it makes sense in an online forum when you can't tell the quality of code it will be pasted into.

我认为这看起来很奇怪。我想在在线论坛中,当您无法判断将要粘贴到的代码质量时,这是有道理的。

Additionally, a MERGEstatement must be terminated by a semi-colon. Do you see a pattern here? These are a couple of the newer additions to TSQL which closely follow SQL Standards. Looks like the SQL Server team are going down the road of mandating the use of the semi-colon terminator.

此外,MERGE语句必须以分号结束。你在这里看到一个模式吗?这些是 TSQL 的几个新添加项,它们紧密遵循 SQL 标准。看起来 SQL Server 团队正在走上强制使用分号终止符的道路。

回答by SQLMenace

GO is a batch terminator, a semi-colon is a statement terminator.

GO 是批处理终止符,分号是语句终止符。

you will use GO when you want to have multiple create proc statements in 1 script because create proc has to be the first statement in a batch. If you use common table expressions then the statement before it needs to be terminated with a semi-colon

当您想在一个脚本中包含多个 create proc 语句时,您将使用 GO,因为 create proc 必须是批处理中的第一个语句。如果您使用公用表表达式,那么它之前的语句需要以分号结束