SQL中GO的用法

來源:互聯網
上載者:User

一、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 ...

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.