【IT168技术文档】
1.TOP 表达式可用在 SELECT、INSERT、UPDATE 和 DELETE 语句中。
2.Top表达式可以是常量,变量,子查询
3.取代set rowcount,可以防止锁升级,提高效率
看看下面的例题:
1create table test2( 2[no] int , 3n nvarchar(100) 4) 5 6insert into test2([no],n) 7select 1,'a' 8union 9select 2,'b' 10union 11select 3,'c' 12union 13select 3,'c2' 14union 15select 2,'b2' 16union 17select 2,'b3' 18go 19 20 21select * 22from test2 23order by [no] asc 24go 25/**//* 26no n 271 a 282 b 292 b2 302 b3 313 c 323 c2 33*/ 34 35select top (2) * 36from test2 37order by [no] asc 38go 39/**//* 40no n 411 a 422 b2 43*/ 44 45-- 利用top变量 46declare @i int 47select @i = 2 48select top (@i) * 49from test2 50order by [no] asc 51go 52/**//* 53no n 541 a 552 b2 56*/ 57 58--利用top子查询 59select top ( select count([no]) from test2 where [no]=3 ) * 60from test2 61order by [no] asc 62go 63/**//* 64no n 651 a 662 b2 67*/ 68 69--指定从基本结果集中返回更多的行,返回的行与TOP n (PERCENT) 行中的最后一行在ORDER BY 列中具有相同的值。 70--只有在指定ORDER BY 子句之后才能指定TOP WITH TIES。 71select top (2) WITH TIES * 72from test2 73order by [no] asc 74go 75/**//* 76no n 771 a 782 b 792 b2 802 b3 81*/