GO 'N' Times, SQL execute the same statement multiple times

GO (Transact-SQL)

 

grammar

 

GO [count]

parameter

count

It is a positive integer. GO specified number of times before the batch will be executed.

 

Source document < http://msdn.microsoft.com/zh-cn/library/ms188037.aspx >

 

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of 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.

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

The scope of local (user-defined) variables is limited to a batch, and cannot be referenced after a GO command.

 

---- the GO later, the parameters can not be used, only hard-coded, the GO a positive integer number.

 

Source document < http://msdn.microsoft.com/zh-cn/library/ms188037.aspx >

Below is an example:

One unbelievable tip for the day for T-SQL users. What a GO statement do in T-SQL.

By definition it is a batch separator. It will run a batch and commit it. But along with that it also accepts one parameter which tells the SQL engine basically how many times to do the job.

For Example,

SELECT * FROM   sys.objects;
GO
3

 

It will show the output THREE times  :)

 

Next Example,

 
CREATE TABLE #ProductTable
(
    ID  
INT          IDENTITY (1, 1),
    Name
VARCHAR (50)
);
 
 
GO

INSERT  INTO #ProductTable (Name)
VALUES                    ('Office XP');
 
GO
3
 
SELECT *
FROM
   #ProductTable;

OUTPUT

clip_image001

 

Source document < http://www.sqllion.com/2011/09/go-n-times/ >

Reproduced in: https: //www.cnblogs.com/henryhappier/p/3214460.html

Guess you like

Origin blog.csdn.net/weixin_34416649/article/details/93537292