Retrieving go and use usages in a 08-sql statement

Source: Internet
Author: User
Tags management studio sql server management sql server management studio

    • GO

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
Such 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 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 Tran When the script for the Sact-sql statement runs in the SQL Server utility, use GO 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, the input after the Ad hoc session or script starts There are statements composed.
Select 1
Select 2
Select 3
GO
-This is a batch

Select 1
GO
Select 2
GO
-This is a two batch
2) The GO command 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.
3) The user must follow the rules for using batch processing. For example, in the same batch, the database cannot be used directly after it is created. The scope of a local (user-defined) variable is limited to one batch and cannot be referenced after the GO command.
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 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) Different batches are executed separately, and one query failure does not affect the other.
Select * from non-existent table
Select 1
--the query executes with an error and does not return any results

Select * from non-existent table
GO
Select 1
GO
--Query execution will error, but will return the result of select 1
5) GO [Count] count is a positive integer. Specifies the number of times that the batch before go executes.
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

6. Supplement:
Select 1
Select * from non-existent table
Select 3
--Query results will be error, but will return the results of select 1

Select * from non-existent table
Select 1
Select 3
--The query results are error-prone and do not return any query results

Select 1
Select * from non-existent table
Selec 3
--The query results will be error-prone and will not return any query results because the third query syntax is incorrect;


Select 1
Select * from non-existent table
GO
Select 3
GO
--Query results will be error, but will return query results for select 1 and select 3

Select * from non-existent table
GO
Select 1
Select 3
GO
--Query results will be error, but will return query results for select 1 and select 3

Select 1
Select * from non-existent table
GO
Selec 3
GO
--Query results will be error, but will return the results of select 1

    • Use

Use

Changing the current operational database


Usage:
Use database name

Cases:
Create DATABASE XX1--Creating XX1 databases
..................
GO
Create DATABASE XX2--Creating XX2 databases
..................
GO
Use XX1--Go to XX1 database
GO
...--operation on the XX1 database.--
GO
Use XX2--Go to XX2 database
GO
...--operation on the XX2 database.--
GO

Use cannot be used directly in stored procedures
But it can be used like this:
CREATE proc Test
As
EXEC (' Use of pubs select * from Jobs ')--or EXEC (use pubs) exec sp_adduser ' xxxx '
Go

--Using the EXEC (UTE) statement with use to change the current database, but changes to the database environment are only valid until the end of the EXECUTE statement, so you must put the following statements and execute together

(Transferred from Http://blog.sina.com.cn/s/blog_5623cddb0100ybbx.html http://blog.csdn.net/nphyez/article/details/41939915)

Retrieving go and use usages in a 08-sql statement

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.