使用GO关键字在SQL Server [英] using GO keyword in SQL Server
问题描述
我知道使用GO关键字。它发送多条语句到SQL Server为一体的集团,而不是由一个发送每条语句之一。我希望我的权利!
i know the use of GO keyword. it sends multiple statements to sql server as a whole group, instead of sending each statement one by one. i hope i am right!
但我想知道,做程序员在实际应用中使用它。就像如果我们创建一个存储过程,那么也做同样的事情,它也编译了代码,使执行计划,并送全组服务器。
but i want to know that do programmers use it in real applications. like if we create a stored procedure, then that also does the same thing,it also compiles the code and make an execution plan , and send the whole group to the server.
所以我们需要在数据库中的对象,如触发器,视图,存储过程的代码来指定GO关键字?
so do we need to specify the GO keyword in the coding of database objects such as triggers, views, stored procedures ?
推荐答案
GO 是用于信令一批结束的命令。请注意,这不是一个T-SQL语句。
GO is a command used for signaling the end of a batch. Note that it is not a T-SQL statement.
批量是在同一时间发送一组一个或多个Transact-SQL语句的从应用程序到SQL Server执行
Batch is a group of one or more Transact-SQL statements sent at the same time from an application to SQL Server for execution
GO是SQL Server非常有用的。虽然,你只需要使用它真正需要的时候。
所以,你需要记住,与定义使用GO命令的批处理一起,您定义范围特定一块T-SQL代码。 。在分批定义的局部变量是特定于该批次
GO is very useful in SQL Server. Though, you need to use it only when is really needed. So, you need to keep in mind that along with defining a batch using the GO command, you define the scope of that specific piece of T-SQL code. The local variables defined in a batch are specific to that batch.
范围例子 -
DECLARE @STRING1 AS VARCHAR(50)
DECLARE @STRING2 AS VARCHAR(50)
SET @STRING1='BATCH 2'
SET @STRING2='BATCH 3'
SELECT @STRING1 AS RESULT
GO
SELECT @STRING2 AS RESULT
GO
运行这会给你错误说@ STRING2未声明。因为变量的范围与GO结束。因此要小心使用GO和巧妙使用
Running this will give you error saying @STRING2 is not declared. Because the scope of that variable ends with the GO. So beware of using GO and use smartly.
来源 - 的 http://aartemiou.blogspot.com/2009/08/using-go-command-in-sql-server.html
这篇关于使用GO关键字在SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!