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中运行通用脚本时,它似乎在各处使用,而不是分号。
5 个解决方案
#1
82
GO
only relates to SSMS - it isn't actual Transact SQL, it just tells SSMS to send the SQL statements between each GO
in individual batches sequentially.
GO只与SSMS相关——它不是实际的事务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.
主要的例外,和地点;最常用的是在公共表表达式语句之前。
#2
67
The reason why you see so many GO's in Generated DDL scripts is because of the following rule about batches.
在生成的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语句必须开始批处理。该批中的所有其他语句将被解释为第一个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.
生成DDL的一个用例是在一个文件中生成多个对象。因此,DDL生成器必须能够生成批。正如其他人所说,GO的声明结束了这批产品。
#3
32
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 GO
statements.
任何变量、表变量等的声明都不能通过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之前使用分号。
#4
9
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 MERGE
statement 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.
此外,合并语句必须由分号终止。你看到规律了吗?这些是TSQL的一些新添加,它们严格遵循SQL标准。看起来,SQL Server团队正沿着强制使用分号终止符的道路前进。
#5
6
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,因为创建proc必须是批处理中的第一个语句。如果使用公共表表达式,则需要用分号终止语句之前的语句
#1
82
GO
only relates to SSMS - it isn't actual Transact SQL, it just tells SSMS to send the SQL statements between each GO
in individual batches sequentially.
GO只与SSMS相关——它不是实际的事务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.
主要的例外,和地点;最常用的是在公共表表达式语句之前。
#2
67
The reason why you see so many GO's in Generated DDL scripts is because of the following rule about batches.
在生成的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语句必须开始批处理。该批中的所有其他语句将被解释为第一个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.
生成DDL的一个用例是在一个文件中生成多个对象。因此,DDL生成器必须能够生成批。正如其他人所说,GO的声明结束了这批产品。
#3
32
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 GO
statements.
任何变量、表变量等的声明都不能通过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之前使用分号。
#4
9
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 MERGE
statement 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.
此外,合并语句必须由分号终止。你看到规律了吗?这些是TSQL的一些新添加,它们严格遵循SQL标准。看起来,SQL Server团队正沿着强制使用分号终止符的道路前进。
#5
6
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,因为创建proc必须是批处理中的第一个语句。如果使用公共表表达式,则需要用分号终止语句之前的语句