日期:2014-05-17  浏览次数:20465 次

双top的问题
SQL code
declare  @table1 table (id int)
insert into @table1
select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 ;
with
a as 
(
    select top(5)id from @table1
)
select top(1)id from a order by id desc

应该以上语句输出为结果5,但是实际结果为8,请问大侠这个是为什么?下面的语句结果也是一样的
SQL code
select top(1)id from( select top(5)id from @table1) a order by id desc


------解决方案--------------------
SQL code
declare  @table1 table (id int)
insert into @table1
select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 ;
with
a as 
(
    select top(5)id from @table1 order by id
)
select top(1)id from a order by id desc
/*

(8 行受影响)
id
-----------
5

(1 行受影响)


*/