SQL Server Management Studio & GO 中的 GO 有什么用?事务 SQL?

发布于 2024-08-22 14:25:04 字数 84 浏览 3 评论 0原文

当我使用右键单击“脚本为”菜单创建查询时,SQL Server Management Studio 总是插入 GO 命令。为什么? GO实际上是做什么的?

SQL Server Management Studio always inserts a GO command when I create a query using the right click "Script As" menu. Why? What does GO actually do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

夜无邪 2024-08-29 14:25:04

它是一个批处理终止符,但是您可以将其更改为您想要的任何内容
替代文字

It is a batch terminator, you can however change it to whatever you want
alt text

好倦 2024-08-29 14:25:04

从 Management Studio 2005 开始,您似乎可以将 GOint 参数一起使用,例如:

INSERT INTO mytable DEFAULT VALUES
GO 10

上面将在 mytable 中插入 10 行。一般来说,GO会执行相关sql命令n

Since Management Studio 2005 it seems that you can use GO with an int parameter, like:

INSERT INTO mytable DEFAULT VALUES
GO 10

The above will insert 10 rows into mytable. Generally speaking, GO will execute the related sql commands n times.

清君侧 2024-08-29 14:25:04

GO 命令不是 Transact-SQL 语句,而是被包括 SQL Server Management Studio 代码编辑器在内的多个 MS 实用程序识别的特殊命令。

GO 命令用于将 SQL 命令分组,然后一起发送到服务器。批次中包含的命令,即自最后一个 GO 命令或会话开始以来的命令集,必须在逻辑上一致。例如,您不能在一个批次中定义变量,然后在另一个批次中使用它,因为变量的范围仅限于定义它的批次。

有关详细信息,请参阅 http://msdn.microsoft.com/en-us/库/ms188037.aspx

The GO command isn't a Transact-SQL statement, but a special command recognized by several MS utilities including SQL Server Management Studio code editor.

The GO command is used to group SQL commands into batches which are sent to the server together. The commands included in the batch, that is, the set of commands since the last GO command or the start of the session, must be logically consistent. For example, you can't define a variable in one batch and then use it in another since the scope of the variable is limited to the batch in which it's defined.

For more information, see http://msdn.microsoft.com/en-us/library/ms188037.aspx.

诗化ㄋ丶相逢 2024-08-29 14:25:04

GO 不是 SQL 关键字。

它是客户端工具(如 SSMS)使用的批次分隔符,用于将整个脚本分成批次。

之前多次回答过... 示例 1

GO is not a SQL keyword.

It's a batch separator used by client tools (like SSMS) to break the entire script up into batches

Answered before several times... example 1

萌︼了一个春 2024-08-29 14:25:04

只是为了添加到现有答案中,当您创建视图时,您必须使用 go 将这些命令分成批次,否则您将收到错误 'CREATE VIEW' 必须是视图中的唯一语句批处理。因此,例如,如果没有 go,您将无法执行以下 sql 脚本

create view MyView1 as
select Id,Name from table1
go
create view MyView2 as
select Id,Name from table1
go

select * from MyView1
select * from MyView2

Just to add to the existing answers, when you are creating views you must separate these commands into batches using go, otherwise you will get the error 'CREATE VIEW' must be the only statement in the batch. So, for example, you won't be able to execute the following sql script without go

create view MyView1 as
select Id,Name from table1
go
create view MyView2 as
select Id,Name from table1
go

select * from MyView1
select * from MyView2
橙幽之幻 2024-08-29 14:25:04

当我希望在处理其他查询之前提交一组查询时,我会使用 GO 关键字。

我可以补充的一件事是,当您在 GO 命令之前声明了一些变量时,您将无法在 GO 命令之后访问这些变量。即

DECLARE @dt DateTime = GETDATE();
UPDATE MyTable SET UpdatedOn = @dt where mycondition = 'myvalue';
GO

-- Below query will raise an error saying the @dt is not declared.
UPDATE MySecondTable SET UpdatedOn = @dt where mycondition = 'myvalue'; -- Must declare the scalar variable "@dt".
GO

更新

我看到,人们要求何时使用Go命令,所以我想,我应该添加为什么我在我的应用程序中使用Go命令查询。

当我在表中有大量更新时,我通常会在下班时运行这些更新(这意味着我不会监视查询),因为第二天来并找到准备好进行其他操作的表很方便。

当我需要运行长时间操作并希望分离查询并完成部分事务时,我使用 Go 命令,例如:

-- First Query
Update MyBigTable1 SET somecol1='someval1' where somecol2='someval2'
GO
-- Second Query
Update MyBigTable2 SET somecol1='someval1' where somecol2='someval2'
GO
-- Third Query
Update MyBigTable3 SET somecol1='someval1' where somecol2='someval2'

执行上述查询将单独提交修改,而不会导致大量回滚日志形成。另外,如果第三个查询失败,您知道前两个查询已正确执行,并且不会回滚任何内容。因此,您不需要花费更多时间再次更新/删除先前执行的查询的记录。

简而言之,“我使用GO命令作为视频游戏中的检查点。”总结如果您在检查点(GO 命令)之后失败,则无需重新开始,而是从最后一个检查点开始游戏。

I use the GO keyword when I want a set of queries to get committed before heading on to the other queries.

One thing I can add is, when you have some variables declared before the GO command you will not be able to access those after the GO command. i.e

DECLARE @dt DateTime = GETDATE();
UPDATE MyTable SET UpdatedOn = @dt where mycondition = 'myvalue';
GO

-- Below query will raise an error saying the @dt is not declared.
UPDATE MySecondTable SET UpdatedOn = @dt where mycondition = 'myvalue'; -- Must declare the scalar variable "@dt".
GO

Update

I see, people requesting when to use the Go command, so I thought, I should add why I use the Go command in my queries.

When I have huge updates in the tables and I usually run these updates while going off from work (which means, I wouldn't be monitoring the queries), since it is convenient to come the next day and find the tables ready for other operations.

I use Go command when I need to run long operations and want to separate the queries and complete part of the transactions such as:

-- First Query
Update MyBigTable1 SET somecol1='someval1' where somecol2='someval2'
GO
-- Second Query
Update MyBigTable2 SET somecol1='someval1' where somecol2='someval2'
GO
-- Third Query
Update MyBigTable3 SET somecol1='someval1' where somecol2='someval2'

Executing above queries will individually commit the modifications without resulting in huge roll-back logs formation. Plus if something fails on third query, you know first 2 queries were properly executed and nothing would be rolled-back. So you do not need to spend more time updating/deleting the records again for the previously executed queries.

To sum it up in just one sentence, "I use the GO command as a check point as in the video games." If you fail after the check point (GO command), you do not need to start over, rather your game starts from the last check point.

别靠近我心 2024-08-29 14:25:04

Go 的意思是,无论在它之前和之前的 GO 之后编写什么 SQL 语句,都将转到 SQL Server 进行处理。

Select * from employees;
GO    -- GO 1

update employees set empID=21 where empCode=123;
GO    -- GO 2

在上面的示例中,GO 1之前的语句将分批转到sql sever,然后GO 2之前的任何其他语句将分批转到sql server。正如我们所看到的,它已经分开了批次。

Go means, whatever SQL statements are written before it and after any earlier GO, will go to SQL server for processing.

Select * from employees;
GO    -- GO 1

update employees set empID=21 where empCode=123;
GO    -- GO 2

In the above example, statements before GO 1 will go to sql sever in a batch and then any other statements before GO 2 will go to sql server in another batch. So as we see it has separated batches.

゛时过境迁 2024-08-29 14:25:04
Use herDatabase
GO ; 

代码表示执行 GO 标记上方的指令。
我的默认数据库是 myDatabase,因此不要使用 myDatabase GO 并使当前查询使用 herDatabase

Use herDatabase
GO ; 

Code says to execute the instructions above the GO marker.
My default database is myDatabase, so instead of using myDatabase GO and makes current query to use herDatabase

这样的小城市 2024-08-29 14:25:04

我没有看到列出的一种用法是错误恢复。
由于一次只运行两个 GO 之间的命令,这意味着一个命令中的编译错误可以与其他命令分开。通常,批处理中的任何编译错误都会导致整个事情无法执行。

exec do.Something
GO
sel from table
print 'here'
GO
print 'there'

在上面,由于“sel”语句中的错误,“here”不会被打印。

现在,在中间添加 GO:

exec do.Something
GO
sel from table
GO
print 'here'
GO
print 'there'

像以前一样,您会收到“sel”错误,但“here”确实会得到输出。

One usage that I haven't seen listed is Error Resilience.
Since only the commands between two GOs are run at a time, that means a compile error in one command can be separated from others. Normally any compile errors in a batch cause the entire thing to not be executed.

exec do.Something
GO
sel from table
print 'here'
GO
print 'there'

In above, 'here' will not be printed because of the error in the 'sel' statement.

Now, adding a GO in the middle:

exec do.Something
GO
sel from table
GO
print 'here'
GO
print 'there'

You get an error for 'sel' as before, but 'here' does get output.

丢了幸福的猪 2024-08-29 14:25:04

tldr; 在现在的大多数情况下,GO 在 IMO 中基本上是可选的。在大型事务批处理中使用 GO 是最好的,在这种情况下,您将在一个大型脚本中编译许多不同的脚本,并且不希望在使用类似变量时出现错误,因此在需要时将部分事务提交到服务器,而不是全部提交到服务器。脚本由于错误而被回滚。

大额交易 1 -->运行成功

GO; -->位于服务器中

LARGE TRANSACTION 2 -->运行成功

GO; -->位于服务器中

LARGE TRANSACTION 3 -->错误

继续; -->如果没有其他 GO 语句,这将回滚事务 1 和事务 2。 2


不确定提供此信息的最佳方式,但我确实觉得到目前为止我所读到的内容并没有真正总结一切,也没有包括我遇到的一个例子。

正如之前多次提到的,GO 只是将一批命令“提交”到服务器。

我认为理解会话也有助于理解 GO 语句的必要性(或可选性)。

(这是我的技术性可能失败的地方,但社区会指出这一点,我们可以使这个答案更好)

通常开发人员在单个会话中工作,并且通常只是对数据库执行简单的语句。在这种情况下,GO 是可选的,而且实际上......人们所要做的就是将其放在语句末尾。

它变得更有帮助的地方可能是 Jamshaid K. 给出的选项,其中您将有许多需要提交的大型事务反过来,而不是当一个事务失败时所有事务都回滚。

这也变得有用的另一种情况(这是我经历过的唯一的其他情况)是许多小事务被编译成一个大脚本。例如

Dev 1 创建脚本 1

Dev 2 创建脚本 2

Dev 1 创建脚本 3

为了部署它们,编写了一个 Python 脚本来组合脚本,因此 Script Master = script1 + script 2 + script 3

。否则,脚本使用冲突变量时可能会出现错误,或者如果脚本 3 失败,脚本 1 和 2 中的事务将回滚。

现在,考虑到当前的 CI/CD 解决方案,这个过程可能已经过时了,但这可能是另一种我可以看到 GO 有所帮助/预期的场景。

tldr; In most cases nowadays GO is mostly IMO optional. Using GO is best in LARGE transaction batches where you would have compiled many different scripts together in a large script and don't want errors where similar variables are used and so that parts of the transaction is committed to the server when desired instead of all of the script being rolled back due to an error.

LARGE TRANSACTION 1 --> Runs Successfully

GO; --> Is in the server

LARGE TRANSACTION 2 --> Runs Successfully

GO; --> Is in the server

LARGE TRANSACTION 3 --> Errors

GO; --> Without the other GO statements this would rollback Transaction 1 & 2


Not sure the best way to provide this SO wise however I do feel like what I've read so far doesn't really sum it all up and include an example that I've come across.

As stated many times before GO simply "commits" a batch of commands to the server.

I think understanding sessions also helps with understanding the necessity (or optionality) of the GO statement.

(This is where my technicality may fail but the community will point it out and we can make this answer better)

Typically developers are working in a single session and typically just executing simple statements to the database. In this scenario GO is optional and really...all one would do is throw it at the end of their statements.

Where it becomes more helpful is probably an option given by Jamshaid K. where you would have many large transactions that you would want committed in turn instead of all transactions being rolled back when one fails.

The other scenario where this also becomes helpful (which is the only other spot I've experienced it) is where many small transactions are compiled into one large script. For example

Dev 1 makes script 1

Dev 2 makes script 2

Dev 1 makes script 3

In order to deploy them a python script is written to combine the scripts so Script Master = script1 + script 2 + script 3.

GO statements would be required in the 3 scripts otherwise there could be errors where the scripts use conflicting variables or if script 3 fails the transactions from scripts 1 and 2 would be rolled back.

Now this process is probably archaic given current CI/CD solutions out there now but that would probably be another scenario where I could see GO being helpful/expected.

黒涩兲箜 2024-08-29 14:25:04

GO 意味着要求 SQL 重复此操作,无论您在其旁边添加什么数字。就像英语里说的一样; “嘿,去那里 3 次。”在 SQL 中尝试以下结果将渲染表 3 次。

    SELECT * FROM Table
    GO 3

GO means asking SQL repeat this whatever number you add next to it. Just like saying in English; "Hey GO there 3 times.". Try below in SQL and the result will be rendering table 3 times.

    SELECT * FROM Table
    GO 3
墨落画卷 2024-08-29 14:25:04

According to docs "GO signals the end of a batch of Transact-SQL statements to the SQL Server utilities". But other than that, it has [count] argument to repeat preceding batch [count] time. Very useful for sequential inserts :)

川水往事 2024-08-29 14:25:04

它是一个分隔查询的命令。如果您正在进行多个选择,则不会产生很大的差异,例如,对我来说,主要用途是当我创建脚本时,您需要创建存储过程,然后授予访问权限或执行它们。例如:

CREATE OR ALTER PROCEDURE dbo.select1
AS
BEGIN
    SET NOCOUNT ON
        
    SELECT 1
END

EXEC dbo.select1

这个它将创建包含所有内容的存储过程,包括 EXEC,并且最终会陷入循环。
这样 GO 就会说在结束后创建存储过程并在执行后执行它。

CREATE OR ALTER PROCEDURE dbo.select1
AS
BEGIN
    SET NOCOUNT ON
        
    SELECT 1
END
GO
EXEC dbo.select1

It is a command to separate queries. If you are doing multiple selects it doesn't make a huge difference, the main use for me for example is when I am creating scripts and you need to create stored procedures and after give access or execute them. For example:

CREATE OR ALTER PROCEDURE dbo.select1
AS
BEGIN
    SET NOCOUNT ON
        
    SELECT 1
END

EXEC dbo.select1

This one it will create the stored procedure with everything on it, including the EXEC and it would end-up in a loop.
So that GO it will say that after end create the stored proc and after execute it.

CREATE OR ALTER PROCEDURE dbo.select1
AS
BEGIN
    SET NOCOUNT ON
        
    SELECT 1
END
GO
EXEC dbo.select1
你穿错了嫁妆 2024-08-29 14:25:04

如果您有 C# 背景。 GO 语句就像我们的 Task.Wait 命令。就像Task.Wait一样,它告诉SQL Server编译器首先完成GO关键字之前的SQL查询的执行,然后继续执行之后的查询。希望有帮助:)

If you are coming from a C# background. GO statement is just like our Task.Wait command. Just like Task.Wait, it tells SQL server compiler to first complete the execution of SQL query before GO keyword and then continue to execute the following queries after that. Hope that helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文