SQL Server 2008中SQL增强之二:Top新用途
SQL Server 2008中SQL应用系列--目录索引一、TOP替代Set RowCount
在SQL Server 2005之前的传统SQL语句中,top语句是不支持局部变量的。见http://www.iyunv.com/downmoon/archive/2007/12/29/1019686.html
此时可以使用Set RowCount,但是在SQL Server 2005/2008中,TOP通常执行得更快,所以应该用TOP关键字来取代Set RowCount。
/***************创建测试表*********************
****************downmoo3w@live.cn ***************/
IF NOT OBJECT_ID('') IS NULL
DROP TABLE
GO
Create table
(PID int identity(1,1) primary key not null
,PName nvarchar(100) null
,AddTime dateTime null
,PGuid Nvarchar(40)
)
go
truncate table
/***************创建1002条测试数据*********************
****************downmoo3w@live.cn ***************/
declare @d datetime
set @d=getdate()
declare @i int
set @i=1
while @i0
begin
delete top (202) from
end
/*
(202 row(s) affected)
(202 row(s) affected)
(202 row(s) affected)
(202 row(s) affected)
(194 row(s) affected)
*/
注意是每批删除202条数据,TOP也可以用于Select和Update语句,其中后者更为实用。
--Select TOP(100)
--Update TOP(100)
页:
[1]