T-SQL中的“;”和“GO”有什么区别?

时间:2022-09-26 16:10:56

I use ADO.NET as well as the sqlcmd utility to send SQL scripts to SQL Server 2008. What is the difference between using ; and GO to separate chunks of SQL?

我使用ADO。以及sqlcmd实用工具,用于将SQL脚本发送到SQL Server 2008。使用的区别是什么?然后去分离SQL块?

7 个解决方案

#1


52  

GO is not actually a T-SQL command. The GO command was introduced by Microsoft tools as a way to separate batch statements such as the end of a stored procedure. GO is supported by the Microsoft SQL stack tools but is not formally part of other tools.

GO实际上不是一个T-SQL命令。GO命令是由Microsoft tools引入的,用于分离批处理语句(如存储过程的结束)。GO是由Microsoft SQL栈工具支持的,但并不是其他工具的正式部分。

You cannot put a GO into a string of SQL and send it as part of a ADO.NET command object as SQL itself does not understand the term. Another way to demonstrate this is with the profiler: set up some statements that use GO in Query Analyzer/Management Studio and then run the profiler when you execute. You will see they are issued as separate commands to the server.

不能将GO放入SQL字符串中并作为ADO的一部分发送。NET命令对象作为SQL本身并不理解这个术语。另一种演示这一点的方法是使用profiler:在Query Analyzer/Management Studio中设置一些使用GO的语句,然后在执行时运行profiler。您将看到它们作为单独的命令分发给服务器。

The semi-colon is used to signify the end of a statement itself, not necessarily a whole batch.

分号用来表示语句本身的结束,不一定是整批语句。

http://msdn.microsoft.com/en-us/library/ms188037.aspx

http://msdn.microsoft.com/en-us/library/ms188037.aspx

#2


29  

"GO" is similar to ; in many cases, but does in fact signify the end of a batch.

“GO”与“GO”类似;在许多情况下,但实际上确实意味着批次的结束。

Each batch is committed when the "GO" statement is called, so if you have:

当调用“GO”语句时,将提交每个批,因此,如果您有:

SELECT * FROM table-that-does-not-exist;
SELECT * FROM good-table;

in your batch, then the good-table select will never get called because the first select will cause an error.

在您的批中,那么good-table select将永远不会被调用,因为第一个select将导致错误。

If you instead had:

如果你有:

SELECT * FROM table-that-does-not-exist
GO
SELECT * FROM good-table
GO

The first select statement still causes an error, but since the second statement is in its own batch, it will still execute.

第一个select语句仍然会导致错误,但是由于第二个语句是在它自己的批处理中,它仍然会执行。

GO has nothing to do with committing a transaction.

GO与提交事务没有任何关系。

#3


21  

semicolon is a statement separator. The previous statement(s) is not necessarily executed when a semicolon is encountered.

分号是一个语句分隔符。当遇到分号时,不需要执行前面的语句。

GO

Signifies the end of a batch. Executes the previous batch of statements, as does encountering the end of the block.

表示批处理的结束。执行前一批语句,就像遇到块的末尾一样。

GO 2

Means execute the batch that many times. I think I've used that option maybe twice in my life. Then again, I'm not a DBA by trade.

意味着多次执行该批处理。我想我已经用过两次了。再说一遍,我不是做商业的DBA。

#4


4  

'GO' is typically used to indicate the end of a batch of SQL statements which means that you could have a begin transaction and end transaction wrapped up into a single collection of statements that could fail or succeed together.

“GO”通常用于指示一批SQL语句的结束,这意味着您可以将一个begin事务和结束事务打包为一个可能一起失败或成功的语句集合。

';' is generally used to separate multiple SQL statements from one another. This is noticable in SQL scripts that need to return multiple recordsets, such as `select * from table1; select * from table2;' which would result in two separate recordsets on the client's side.

';'通常用于将多个SQL语句相互分离。这在需要返回多个记录集的SQL脚本中很明显,例如' select * from table1;从表2中选择*;',会在客户端产生两个独立的记录集。

#5


4  

  1. Under SQL Server TSQL (2005 - 2016) bear in mind that:

    在SQL Server TSQL(2005 - 2016)下,请记住:

    • Semicolon (;) is a block terminator.
    • 分号(;)是块终止符。
    • GO is a batch terminator.
    • GO是一个批处理终止符。
  2. Additionally, GO can be used to invoke the same DML block multiple times using the following syntax:

    此外,GO可以使用以下语法多次调用相同的DML块:

GO [count]

去(计数)

Where [count] is a positive integer that indicates how many times the TSQL block of commands preceding said GO are to be carried out over and over.

其中[count]是一个正整数,表示在GO之前执行的命令的TSQL块要执行多少次。

  1. Also, unlike semicolon, GO is mandatory before a new DDL, say, when you create a new view, since a semicolon separating previous commands will trigger an error. For example:
  2. 此外,与分号不同,GO在新的DDL之前是必需的,比如在创建新视图时,因为分号分隔以前的命令会引发错误。例如:

drop view #temporary_view
GO
create view #another_view...
--> NO ERRORS

删除视图#temporary_view去创建视图#another_view…- - >没有错误

If you replaced GO with a semicolon in the previous example, it will raise the following error message:

如果您将GO替换为前一个示例中的分号,它将引发以下错误消息:

'CREATE VIEW' must be the first statement in a query batch.

“创建视图”必须是查询批处理中的第一个语句。

#6


1  

The command GO means the end of a batch. Therefore all variables declared before GO are invalid after the GO command. Against the semicolon does not end the batch.

GO命令表示批处理的结束。因此,在GO之前声明的所有变量在GO命令之后都无效。对分号不结束批次。

If You will use a DML command in a procedure, use the semicolon instead GO. For example:

如果要在过程中使用DML命令,则使用分号。例如:

CREATE PROCEDURE SpMyProc
@myProcParam VARCHAR(20)
AS
DECLARE @myOtherParam INT = 5
;DISABLE TRIGGER ALL ON tMyTable
UPDATE tMyTable SET myVar = @myProcParam, mySecondVar = @myOtherParam
;ENABLE TRIGGER OLL ON tMyTable 

#7


0  

I thought the ; character separates a list of SQL commands, GO just instructs SQL Server to commit all the previous commands.

我认为;字符分隔SQL命令列表,GO只指示SQL Server提交所有以前的命令。

#1


52  

GO is not actually a T-SQL command. The GO command was introduced by Microsoft tools as a way to separate batch statements such as the end of a stored procedure. GO is supported by the Microsoft SQL stack tools but is not formally part of other tools.

GO实际上不是一个T-SQL命令。GO命令是由Microsoft tools引入的,用于分离批处理语句(如存储过程的结束)。GO是由Microsoft SQL栈工具支持的,但并不是其他工具的正式部分。

You cannot put a GO into a string of SQL and send it as part of a ADO.NET command object as SQL itself does not understand the term. Another way to demonstrate this is with the profiler: set up some statements that use GO in Query Analyzer/Management Studio and then run the profiler when you execute. You will see they are issued as separate commands to the server.

不能将GO放入SQL字符串中并作为ADO的一部分发送。NET命令对象作为SQL本身并不理解这个术语。另一种演示这一点的方法是使用profiler:在Query Analyzer/Management Studio中设置一些使用GO的语句,然后在执行时运行profiler。您将看到它们作为单独的命令分发给服务器。

The semi-colon is used to signify the end of a statement itself, not necessarily a whole batch.

分号用来表示语句本身的结束,不一定是整批语句。

http://msdn.microsoft.com/en-us/library/ms188037.aspx

http://msdn.microsoft.com/en-us/library/ms188037.aspx

#2


29  

"GO" is similar to ; in many cases, but does in fact signify the end of a batch.

“GO”与“GO”类似;在许多情况下,但实际上确实意味着批次的结束。

Each batch is committed when the "GO" statement is called, so if you have:

当调用“GO”语句时,将提交每个批,因此,如果您有:

SELECT * FROM table-that-does-not-exist;
SELECT * FROM good-table;

in your batch, then the good-table select will never get called because the first select will cause an error.

在您的批中,那么good-table select将永远不会被调用,因为第一个select将导致错误。

If you instead had:

如果你有:

SELECT * FROM table-that-does-not-exist
GO
SELECT * FROM good-table
GO

The first select statement still causes an error, but since the second statement is in its own batch, it will still execute.

第一个select语句仍然会导致错误,但是由于第二个语句是在它自己的批处理中,它仍然会执行。

GO has nothing to do with committing a transaction.

GO与提交事务没有任何关系。

#3


21  

semicolon is a statement separator. The previous statement(s) is not necessarily executed when a semicolon is encountered.

分号是一个语句分隔符。当遇到分号时,不需要执行前面的语句。

GO

Signifies the end of a batch. Executes the previous batch of statements, as does encountering the end of the block.

表示批处理的结束。执行前一批语句,就像遇到块的末尾一样。

GO 2

Means execute the batch that many times. I think I've used that option maybe twice in my life. Then again, I'm not a DBA by trade.

意味着多次执行该批处理。我想我已经用过两次了。再说一遍,我不是做商业的DBA。

#4


4  

'GO' is typically used to indicate the end of a batch of SQL statements which means that you could have a begin transaction and end transaction wrapped up into a single collection of statements that could fail or succeed together.

“GO”通常用于指示一批SQL语句的结束,这意味着您可以将一个begin事务和结束事务打包为一个可能一起失败或成功的语句集合。

';' is generally used to separate multiple SQL statements from one another. This is noticable in SQL scripts that need to return multiple recordsets, such as `select * from table1; select * from table2;' which would result in two separate recordsets on the client's side.

';'通常用于将多个SQL语句相互分离。这在需要返回多个记录集的SQL脚本中很明显,例如' select * from table1;从表2中选择*;',会在客户端产生两个独立的记录集。

#5


4  

  1. Under SQL Server TSQL (2005 - 2016) bear in mind that:

    在SQL Server TSQL(2005 - 2016)下,请记住:

    • Semicolon (;) is a block terminator.
    • 分号(;)是块终止符。
    • GO is a batch terminator.
    • GO是一个批处理终止符。
  2. Additionally, GO can be used to invoke the same DML block multiple times using the following syntax:

    此外,GO可以使用以下语法多次调用相同的DML块:

GO [count]

去(计数)

Where [count] is a positive integer that indicates how many times the TSQL block of commands preceding said GO are to be carried out over and over.

其中[count]是一个正整数,表示在GO之前执行的命令的TSQL块要执行多少次。

  1. Also, unlike semicolon, GO is mandatory before a new DDL, say, when you create a new view, since a semicolon separating previous commands will trigger an error. For example:
  2. 此外,与分号不同,GO在新的DDL之前是必需的,比如在创建新视图时,因为分号分隔以前的命令会引发错误。例如:

drop view #temporary_view
GO
create view #another_view...
--> NO ERRORS

删除视图#temporary_view去创建视图#another_view…- - >没有错误

If you replaced GO with a semicolon in the previous example, it will raise the following error message:

如果您将GO替换为前一个示例中的分号,它将引发以下错误消息:

'CREATE VIEW' must be the first statement in a query batch.

“创建视图”必须是查询批处理中的第一个语句。

#6


1  

The command GO means the end of a batch. Therefore all variables declared before GO are invalid after the GO command. Against the semicolon does not end the batch.

GO命令表示批处理的结束。因此,在GO之前声明的所有变量在GO命令之后都无效。对分号不结束批次。

If You will use a DML command in a procedure, use the semicolon instead GO. For example:

如果要在过程中使用DML命令,则使用分号。例如:

CREATE PROCEDURE SpMyProc
@myProcParam VARCHAR(20)
AS
DECLARE @myOtherParam INT = 5
;DISABLE TRIGGER ALL ON tMyTable
UPDATE tMyTable SET myVar = @myProcParam, mySecondVar = @myOtherParam
;ENABLE TRIGGER OLL ON tMyTable 

#7


0  

I thought the ; character separates a list of SQL commands, GO just instructs SQL Server to commit all the previous commands.

我认为;字符分隔SQL命令列表,GO只指示SQL Server提交所有以前的命令。