• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

SQL中GO的用法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、GO命令SQL帮助原文如下:

Signals the end of a batch of Transact-SQL statements to the Microsoft® SQL Server™ utilities.

Syntax

GO

Remarks

GO is not a Transact-SQL statement; it is a command recognized by the osql and isql utilities and SQL Query Analyzer.

SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to SQL Server. The current batch of statements is composed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO. SQL Query Analyzer and the osql and isql command prompt utilities implement GO differently. For more information, see osql Utility, isql Utility, and SQL Query Analyzer.

A Transact-SQL statement cannot occupy the same line as a GO command. However, the line can contain comments.

Users must follow the rules for batches. For example, any execution of a stored procedure after the first statement in a batch must include the EXECUTE keyword. The scope of local (user-defined) variables is limited to a batch, and cannot be referenced after a GO command.

USE pubs 
GO 
DECLARE @MyMsg VARCHAR(50) 
SELECT @MyMsg = 'Hello, World.'
GO -- @MyMsg is not valid after this GO ends the batch. 
-- Yields an error because @MyMsg not declared in this batch. 
PRINT @MyMsg 
GO 
SELECT @@VERSION; 
-- Yields an error: Must be EXEC sp_who if not first statement in 
-- batch. 
sp_who
GO

SQL Server applications can send multiple Transact-SQL statements to SQL Server for execution as a batch. The statements in the batch are then compiled into a single execution plan. Programmers executing ad hoc statements in the SQL Server utilities, or building scripts of Transact-SQL statements to run through the SQL Server utilities, use GO to signal the end of a batch.

Applications based on the DB-Library, ODBC, or OLE DB APIs receive a syntax error if they attempt to execute a GO command. The SQL Server utilities never send a GO command to the server.

Permissions

GO is a utility command that requires no permissions. It can be executed by any user.

Examples

This example creates two batches. The first batch contains only a USE pubs statement to set the database context. The remaining statements use a local variable, so all local variable declarations must be grouped in a single batch. This is done by not having a GO command until after the last statement that references the variable.

USE pubs 
GO 
DECLARE @NmbrAuthors int
SELECT @NmbrAuthors = COUNT(*) 
FROM authors 
PRINT 'The number of authors as of ' + 
CAST(GETDATE() AS char(20)) + ' is ' + 
CAST(@NmbrAuthors AS char (10)) 
GO

二、简要说明

  GO 表示一批 T-SQL 语句结束,GO 之后的 T-SQL 语句属于另一个批处理的范围,在 T-SQL 所有语句的最后都默认有一个 GO。但是,请注意 GO 不是 T-SQL 语句,而只是一个能被SQL Server 实用工具识别的命令。

use pubs
create database ...

  上例会发生错误,因为 create trigger 语句必须是批处理的第一句,而上述示例中批处理的第一句为 use pubs,应该如下写:

use pubs
go
create database ...


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
go开发目录配置-gomod发布时间:2022-07-10
下一篇:
[GO]接口的定义和实现发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap