什么是“批处理",为什么要使用 GO? [英] What is a "batch", and why is GO used?

查看:33
本文介绍了什么是“批处理",为什么要使用 GO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读并阅读了 MSDN 等.好的,所以它表示批处理结束.

什么定义了批次?当我粘贴一堆脚本同时运行时,我不明白为什么我需要去.

我从不理解 GO.任何人都可以更好地解释这一点以及我何时需要使用它(在多少或什么类型的交易之后)?

例如,为什么每次更新后我都需要 GO:

 更新 [国家]SET [国家代码] = 'IL'WHERE 代码 = 'IL'走更新 [国家]SET [国家代码] = 'PT'WHERE 代码 = 'PT'

解决方案

GO 不是正确的 TSQL 命令.

相反,它是对连接到 SQL 服务器(Sybase 或 Microsoft 的 - 不确定 Oracle 做什么)的特定客户端程序的命令,向客户端程序发出信号,表明该组命令是输入它直到go"需要发送到服务器执行.

为什么/什么时候需要它?

  • GO 在 MS SQL 服务器中有一个计数"参数 - 因此您可以将其用作重复 N 次";快捷方式.

  • 非常大的更新可能会填满 SQL 服务器的日志.为避免这种情况,可能需要通过 go 将它们分成更小的批次.

    在您的示例中,如果一组国家/地区代码的更新量很大,以至于会耗尽日志空间,则解决方案是将每个国家/地区代码分离到一个单独的事务中 - 这可以通过在使用 go 的客户端.

  • 某些 SQL 语句必须通过 GO 与以下语句分开才能工作.

    例如,您不能在单个事务中删除表并重新创建同名表,至少在 Sybase 中(创建过程/触发器同上):

<块引用>

<代码>>删除表 tempdb.guest.x1>创建表 tempdb.guest.x1 (a int)>走消息 2714,级别 16,状态 1服务器SYBDEV",第 2 行数据库中已经有一个名为x1"的对象.>删除表 tempdb.guest.x1>走>创建表 tempdb.guest.x1 (a int)>走>

I have read and read over MSDN, etc. Ok, so it signals the end of a batch.

What defines a batch? I don't see why I need go when I'm pasting in a bunch of scripts to be run all at the same time.

I've never understood GO. Can anyone explain this better and when I need to use it (after how many or what type of transactions)?

For example why would I need GO after each update here:

 UPDATE [Country]
   SET [CountryCode] = 'IL'
 WHERE code = 'IL'

 GO

 UPDATE [Country]
   SET [CountryCode] = 'PT'
 WHERE code = 'PT'

解决方案

GO is not properly a TSQL command.

Instead it's a command to the specific client program which connects to an SQL server (Sybase or Microsoft's - not sure about what Oracle does), signalling to the client program that the set of commands that were input into it up till the "go" need to be sent to the server to be executed.

Why/when do you need it?

  • GO in MS SQL server has a "count" parameter - so you can use it as a "repeat N times" shortcut.

  • Extremely large updates might fill up the SQL server's log. To avoid that, they might need to be separated into smaller batches via go.

    In your example, if updating for a set of country codes has such a volume that it will run out of log space, the solution is to separate each country code into a separate transaction - which can be done by separating them on the client with go.

  • Some SQL statements MUST be separated by GO from the following ones in order to work.

    For example, you can't drop a table and re-create the same-named table in a single transaction, at least in Sybase (ditto for creating procedures/triggers):

> drop table tempdb.guest.x1          
> create table tempdb.guest.x1 (a int)
> go
  Msg 2714, Level 16, State 1
  Server 'SYBDEV', Line 2
  There is already an object named 'x1' in the database.   
  
> drop table tempdb.guest.x1          
> go
> create table tempdb.guest.x1 (a int)
> go
>

这篇关于什么是“批处理",为什么要使用 GO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆