TABLE:1 asdf
2 斯蒂芬
3 地地道道
4 ggg
求查出   3 地地道道
4 ggg
就是差最后N条记录,要顺序的不要倒序。。谢谢

解决方案 »

  1.   

    如果原数据是按第一列升序排列的,可以用下面的。declare @TABLE table ([cl1] int,[cl2] varchar(8))
    insert into @TABLE
    select 1,'asdf' union all
    select 2,'斯蒂芬' union all
    select 3,'地地道道' union all
    select 4,'ggg'declare @n smallint
    set @n=2select *
    from
    (select top (@n) * from @TABLE order by cl1 desc)t
    order by cl1cl1         cl2
    ----------- --------
    3           地地道道
    4           ggg
      

  2.   

    -->Title:Generating test data
    -->Author:happy_stone【不會飛的石頭】
    -->Date :2009-10-20 07:51:15
     
    if not object_id('Tempdb..#t') is null
    drop table #t
    Go
    Create table #t([Col1] int,[Col2] nvarchar(4))
    Insert #t
    select 1,N'asdf' union all
    select 2,N'斯蒂芬' union all
    select 3,N'地地道道' union all
    select 4,N'ggg'
    Go
    declare @N int
    set @n=2
    select * from(
    select top (@n) * from #t order by col1 desc)t
    order by col1 asc
    /*
    Col1        Col2
    ----------- ----
    3           地地道道
    4           ggg(2 個資料列受到影響)
    */
      

  3.   

    -->Title:Generating test data
    -->Author:happy_stone【不會飛的石頭】
    -->Date :2009-10-20 07:51:15
     
    if not object_id('Tempdb..#t') is null
    drop table #t
    Go
    Create table #t([Col1] int,[Col2] nvarchar(4))
    Insert #t
    select 1,N'asdf' union all
    select 2,N'斯蒂芬' union all
    select 3,N'地地道道' union all
    select 4,N'ggg'
    Go
    --MSSQL2000
    declare @N int
    set @n=2
    exec('select * from(
    select top '+@n+' * from #t order by col1 desc)t
    order by col1 asc')
    /*
    Col1        Col2
    ----------- ----
    3           地地道道
    4           ggg(2 個資料列受到影響)
    */