简体   繁体   English

TSQL中GO语句的含义是什么?

[英]What's the meaning of the GO-statement in TSQL

I'm new to TSQL and wondering what the GO statement really means. 我是TSQL的新手,想知道GO语句的真正含义。 To me it just seems thrown in there where ever it seems to fit. 对我来说,它似乎只是在那里似乎适合。

I guess it somehow tells sql server to run the previous statement? 我想它以某种方式告诉sql server运行前一个语句? What happens if you don't use them at all? 如果您根本不使用它们会发生什么? Could someone give an example where a statement will break if I don't use a GO . 如果我不使用GO有人可以给出一个声明将会中断的示例。

Please elaborate. 请详细说明。

It is a batch terminator 它是批量终止符

this will break 这会打破

declare @i int
set @i =5

declare @i int
set @i =6

Select @i

Msg 134, Level 15, State 1, Line 5 The variable name '@i' has already been declared. 消息134,级别15,状态1,行5变量名称'@i'已经声明。 Variable names must be unique within a query batch or stored procedure. 变量名在查询批处理或存储过程中必须是唯一的。

this will work 这会奏效

declare @i int
set @i =5

go
declare @i int
set @i =6

Select @i

It ends the batch. 它结束批次。

It is rarely needed and is used far more often than it should be. 它很少需要,而且使用频率远远超过它应该使用的频率。 One valid place is in stored proc programming where you first do a check if the proc exists and drop it if it doesn't. 一个有效的地方是存储过程编程,你首先检查proc是否存在,如果不存在则删除它。 Then you use a go statement to end the batch because the create proc statement must be the first statement of a batch. 然后使用go语句结束批处理,因为create proc语句必须是批处理的第一个语句。

By the way. 顺便说说。 It isn't actually a TSQL command. 它实际上不是TSQL命令。 It is just used by Microsoft DB tools to separate two batches of commands. 它只是由Microsoft DB工具用于分隔两批命令。

In fact you can configure it to be a different word in SSMS if you want to under the Optionss..Batch separation preference setting. 实际上,如果您想在Optionss..Batch分离首选项设置下,可以将其配置为SSMS中的不同单词。

The main place this distinction is important is that SQL Will choke on GO statement if you try to use them in queries through a database connection from code, for example through ADO.NET. 这种区别很重要的主要地方是,如果您尝试通过代码中的数据库连接(例如通过ADO.NET)在查询中使用它们,那么SQL将会阻塞GO语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM