GO ‘N’ Times,SQL执行同一个语句多次

GO (Transact-SQL)

 

语法

 

GO [count]

参数

count

为一个正整数。 GO 之前的批处理将执行指定的次数。

 

源文档 <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.

 

---- GO后面,不可以使用参数,只能硬编码,GO一个正整数次数。

 

源文档 <http://msdn.microsoft.com/zh-cn/library/ms188037.aspx>

下面是一个例子:

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

 

源文档 <http://www.sqllion.com/2011/09/go-n-times/>

转载于:https://www.cnblogs.com/henryhappier/p/3214460.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值