Go in SQL Server

Source: Internet
Author: User
Tags getdate create database management studio sql server management sql server management studio

1. Function:
sends a signal to the SQL Server utility to end a batch of Transact-SQL statements.
2. Syntax:
batch of Transact-SQL statements
GO
as
Select 1
Select 2
Select 3
GO
3. Description:
1) GO is not a Transact-SQL statement;
2) It is a command that is recognized by the SQLCMD and osql utilities as well as the SQL Server Management Studio Code Editor.
3) SQL Server applications can execute multiple Transact-SQL statements as a wholesale send to an instance of SQL Server. The statements in the batch are then compiled into an execution plan. Programmers execute special statements in SQL Server utilities, or generate When a script for a Transact-SQL statement runs in the SQL Server utility, GO is used as a signal to end the batch.
4) If an application that is based on an ODBC or OLE DB API tries to execute the GO command, you receive a syntax error. The SQL Server utility never sends a GO command to the server.
4. Permissions:
GO is a utility command that does not require any permissions. It can be executed by any user.
5. Usage:
1) The SQL Server utility interprets GO as a signal that the current batch of Transact-SQL statements should be sent to the SQL Server instance. The current batch statement consists of all the statements entered after the previous GO command, and if it is the first go command, after the ad hoc session or script starts Consists of all the statements entered.

Considerations for using GO statements in SQL Server

1. Go commands and Transact-SQL statements cannot be on the same line. But you can include comments on the GO command line


Select 1
GO
--the result is correctly output and is listed as "no column name"

Select 1 GO

--the result is correctly output, and the column is named "GO"
--The go does not work as it should.


2. Users must follow the rules for using batch processing. For example, in the same batch, the database cannot be used directly after it is created


Create DATABASE [ROC]
Use [Roc]
GO
--After running this batch, the system will error
--Divide this batch into two batches to run correctly


Create DATABASE [ROC]
GO
Use [Roc]
GO

3. Local (user-defined) variables are scoped to a batch and cannot be referenced after the GO command

DECLARE @Roc varchar (max)
Select @Roc = ' aking '
GO
Print @Roc
GO

--This is two batches, because the scope of the local variable is limited to one batch, so this statement is wrong
--Combine these two batches into a single batch to run correctly

DECLARE @Roc varchar (max)
Select @Roc = ' aking '
Print @Roc
GO

4. Each statement that is delimited by Go is a separate transaction, and a statement execution failure does not affect other statement execution

SELECT * from sysobjects where id=a
Select GETDATE ()

--You will find an error and will not show any result set


SELECT * from sysobjects where id=a
Go
Select GETDATE ()
Go

--you will find that the results of select GETDATE () are included in the result set, although the same error will occur.


5, GO [Count] count is a positive integer. Specifies the number of times a batch is executed before go

Insert [Roc] Select ' aking '
GO
--Batch execution 1 times to insert a row of records into the table Roc

Aking Insert [Roc] Select ' aking '
GO 10
--Batch execution 10 times to insert 10 rows of records into the table Roc aking

Go

in SQL Server

Related Article

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.