select *,IDENTITY(int,1,1) as id into #t from tabselect a,b from #t where id in (1,4)
如果需要随机两条,最好 select top 2 * from tab order by newid()
看WHERE条件输的是什么,要显示和条件相关的两条记录.如果超出了两条记录也只显示两条.
--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
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 行受影响)
into #t
from tabselect a,b from #t where id in (1,4)
select top 2 * from tab order by newid()
看WHERE条件输的是什么,要显示和条件相关的两条记录.如果超出了两条记录也只显示两条.
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
(
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 行受影响)