SQL服务器正在获取随机数目的随机行



有没有办法从SQL server表中随机获得N行?我看到了这个链接,但那不是我想要的。每次运行查询时,我都希望获得不同数量的行,例如,5行、10行等

您可以使用这样的东西:

select t.*
from (select t.*, row_number() over (order by (select null)) as seqnum
from t 
) t cross join
(select count(*) as cnt, rand() as rnd from t) x
where seqnum < cnt * rnd;

GSerg写了以下评论:

select top (cast(rand() * 10 as int)) * from table

最新更新