GO in SQLServer

Source: Internet
Author: User
Tags sql server query
The following two sets of SQL Server statements use go and do not use go. (1) createtableaa (col1int, col2char (10) gocreatetablebb (col1int, col2char (11) (2) createtableaa (col1int, col2char (10) createtablebb (col1int, col2char (11) consult experts

The following two sets of SQL Server statements use go and do not use go. (1) create table aa (col1 int, col2 char (10) go create table bb (col1 int, col2 char (11) (2) create table aa (col1 int, col2 char (10) create table bb (col1 int, col2 char (11)

The following two sets of SQL Server statements use go and do not use go.
(1)
Create table aa (col1 int, col2 char (10 ))
Go
Create table bb (col1 int, col2 char (11 ))
(2)
Create table aa (col1 int, col2 char (10 ))
Create table bb (col1 int, col2 char (11 ))
What is the difference between using go and not using go? Thank you for your help.

What is the difference between using go and not using go?
-- Use go to split the SQL statement you want to execute into a part to execute Top


Batch Processing(See books online)
Batch processing is a group containing one or more Transact-SQL statements, which are sent from an application to Microsoft? SQL Server? Run. SQL Server compiles batch processing statements into an executable unit, which is called an execution plan. The statement in the execution plan executes one statement at a time.

Compilation errors (such as syntax errors) make the execution plan unable to be compiled, resulting in the failure to execute any statements in the batch.

Running errors (such as Arithmetic overflow or violation of constraints) have one of the following two effects:

Most running errors stop executing the current statement and subsequent statements in the batch processing. (If an error occurs, do not execute the following statement .)

A few running errors (such as violation of constraints) only stop executing the current statement. Continue to execute all other statements in the batch processing.
The statements executed before a running error occurs are not affected. The only exception is that the transaction rollback is caused by batch processing in the transaction and errors. In this case, uncommitted data modifications made before the rollback runtime error.

It is assumed that there are 10 statements in the batch processing. If the Fifth statement has a syntax error, no statements in the batch are executed. If a batch is compiled and the second statement fails to be executed, the result of the first statement is not affected because it has been executed.

The following rules apply to batch processing:

The create default, create procedure, create rule, create trigger, and create view statements cannot be combined with other statements in batch processing. Batch Processing must begin with the CREATE statement. All other statements that follow the batch processing will be interpreted as part of the first CREATE statement definition.


You cannot change the table in the same batch and then reference the new column.


If the EXECUTE statement is the first sentence in a batch, The EXECUTE keyword is not required. If the EXECUTE statement is not the first statement in batch processing, the EXECUTE keyword is required.

GO(See books online)

Use a signal to notify Microsoft? SQL Server? The end of a batch of Transact-SQL statements.

Syntax
GO

Note
GO is not a Transact-SQL statement, but a command recognized by osql and isql utilities and SQL Server Query analyzer.

The SQL Server utility interprets GO as a signal that the current Transact-SQL batch processing statement should be sent to SQL Server. The current batch processing statement is all the statements entered after the previous GO command. If it is the first GO command, it is all the statements from the beginning of a special session or script to the beginning of this GO command. The SQL query analyzer differs from the osql and isql commands by prompting the utility to execute the GO command in different ways. For more information, see osql utility, isql utility, and SQL query analyzer.

The GO command and the Transact-SQL statement cannot be on the same line. However, the GO command line can contain comments.

You must follow the batch processing rules. For example, after the first statement in the batch processing, any stored procedure must contain the EXECUTE keyword. The scope of local (User-Defined) variables is limited to one batch and cannot be referenced after the 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 as one batch to SQL Server for execution. The statements in this batch are compiled into an execution plan. The programmer executes a specific statement in the SQL Server utility or generates a Transact-SQL statement script to run in the SQL Server utility, and uses GO to identify the end of batch processing.

If an application based on DB-Library, ODBC, or ole db APIs tries to execute the GO command, it will receive a syntax error. SQL Server utility never sends GO commands to the Server.

Permission
GO is a utility command without permissions. It can be executed by any user.

The GO statement is at the end of the stored procedure.

You cannot call GO in Begin... end.

Reference: English (http://technet.microsoft.com/zh-cn/library/ms190487 (SQL .90). aspx)

Http://msdn.microsoft.com/en-us/library/ms190487.aspx)

Although all the Transact-SQL statements are valid in the BEGIN... END block, some Transact-SQL statements should not be grouped into the same batch of processing or statement blocks. For more information, see batch processing and various statements.

The following rules apply to batch processing:

  • The create default, create function, create procedure, create rule, create schema, create trigger, and create view statements cannot be combined with other statements in batch processing. Batch Processing must begin with the CREATE statement. All other statements that follow the batch processing will be interpreted as part of the first CREATE statement definition.
  • You cannot change the table in the same batch and then reference the new column.
  • If the EXECUTE statement is the first sentence in a batch, The EXECUTE keyword is not required. If the EXECUTE statement is not the first statement in batch processing, the EXECUTE keyword is required.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.