";" 和有什么不一样 和 T-SQL 中的“GO”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1517527/
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 03:52:23  来源:igfitidea点击:

What is the difference between ";" and "GO" in T-SQL?

sqlsql-serversql-server-2008tsql

提问by Nestor

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 GOto separate chunks of SQL?

我使用 ADO.NET 以及 sqlcmd 实用程序将 SQL 脚本发送到 SQL Server 2008。使用;GO分隔 SQL 块有什么区别?

回答by keithwarren7

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

GO实际上不是 T-SQL 命令。该GO命令是由 Microsoft 工具引入的,用于分隔批处理语句,例如存储过程的结尾。GO受 Microsoft SQL 堆栈工具支持,但不是其他工具的正式组成部分。

You cannot put a GOinto 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 GOin Query Analyzer/Management Studio and then run the profiler when you execute. You will see they are issued as separate commands to the server.

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

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

回答by Phillip Knauss

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

“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.

在您的批处理中,那么好的表选择将永远不会被调用,因为第一个选择会导致错误。

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 与提交事务无关。

回答by Bob Kaufman

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。

回答by alejandrob

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

    • Semicolon (;) is a blockterminator.
    • GO is a batchterminator.
  2. Additionally, GO can be used to invoke the same DML block multiple times using the following syntax:

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

    • 分号 (;) 是终止符。
    • GO 是一个批处理终止器。
  2. 此外,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 mandatorybefore a new DDL, say, when you create a new view, since a semicolon separating previous commands will trigger an error. For example:
  1. 此外,与分号不同,GO在新 DDL 之前是强制性的,例如,当您创建新视图时,因为分隔先前命令的分号会触发错误。例如:

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

删除视图#temporary_view
GO
创建视图#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.

'CREATE VIEW' 必须是查询批处理中的第一条语句。

回答by Mike J

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

“GO”通常用于表示一批SQL 语句的结束,这意味着您可以将 abegin transactionend transaction包装成单个语句集合,这些语句可能一起失败或成功。

';' 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; select * from table2;' 这将导致客户端有两个单独的记录集。

回答by valerius.kaiser

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 命令,请使用分号代替 GO。例如:

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 

回答by Phil.Wheeler

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 提交所有先前的命令。