首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

双top的有关问题

2012-10-15 
双top的问题SQL codedeclare@table1 table (id int)insert into @table1select 1 union select 2 union se

双top的问题

SQL code
declare  @table1 table (id int)insert into @table1select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 ;witha 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 @table1select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 ;witha as (    select top(5)id from @table1 order by id)select top(1)id from a order by id desc/*(8 行受影响)id-----------5(1 行受影响)*/ 

热点排行
Bad Request.