有如下表 
A    B 
111  1 
111  2 
222  3 
222  4 
222  5 
333  6 
333  7 我想每次查询的时候只显两条数据,但不能用TOP 2,不知还有没有其它方法,如果加一列自动编号不知能不能实现.但我不会加.请各位指点.

解决方案 »

  1.   

    select *,IDENTITY(int,1,1) as id
    into #t 
    from tabselect a,b from #t where id in (1,4)
      

  2.   

    如果需要随机两条,最好
    select top 2 * from tab order by newid()
      

  3.   


    看WHERE条件输的是什么,要显示和条件相关的两条记录.如果超出了两条记录也只显示两条.
      

  4.   

    --sql 2000
    select * where b <= 2 select a , b from (select * , px = (select count(1) from tb where b < t.b) + 1 from tb t) m where px <= 2--sql 2005
    select a , b from (select * , px = row_number() over(order by a , b)) m where px <= 2
      

  5.   

    create table #T
    (
      A varchar(20),
      B int
    )
    insert into #T select '111',1
    union all select '111',2
    union all select '222',3
    union all select '222',4
    union all select '222',5
    union all select '333',6
    union all select '333',7 select *,row_number() over (order by B) B from #T where B<=2
    A                    B           B
    -------------------- ----------- --------------------
    111                  1           1
    111                  2           2(2 行受影响)
      

  6.   

    select * from t where rownum<3;