“ORDER BY(SELECT NULL)”是什么意思?

下面的SQL是从Itzik Ben-Gan用来生成一个数字表。 order by (select null)部分的order by (select null)是什么意思? 谢谢。

 DECLARE @number_of_numbers INT; SELECT @number_of_numbers = 100000; WITH a AS ( SELECT 1 AS i UNION ALL SELECT 1 ), b AS ( SELECT 1 AS i FROM a AS x , a AS y ), c AS ( SELECT 1 AS i FROM b AS x , b AS y ), d AS ( SELECT 1 AS i FROM c AS x , c AS y ), e AS ( SELECT 1 AS i FROM d AS x , d AS y ), f AS ( SELECT 1 AS i FROM e AS x , e AS y ), numbers AS ( SELECT TOP ( @number_of_numbers ) ROW_NUMBER() OVER ( ORDER BY ( SELECT NULL ) ) AS number FROM f ) SELECT * FROM numbers; 

谢谢!

ROW_NUMBER语法上需要一个ORDER BY子句。 没有一个你不能使用它。 SELECT NULL是一个窍门,在不执行任何特定顺序的情况下closures错误。 在这种情况下,我们不需要执行任何顺序,所以最快的select是使用SELECT NULL

优化器可以看穿这个技巧,所以它没有运行成本(通过查看执行计划可以很容易地validation此声明)。