What does “ORDER BY (SELECT NULL)” mean?

后端 未结 1 476
慢半拍i
慢半拍i 2020-12-02 18:22

The following SQL is from Itzik Ben-Gan that is used to generate a numbers table. What does the order by (select null) part mean? Thanks.

DECLAR         


        
相关标签:
1条回答
  • 2020-12-02 19:14

    ROW_NUMBER requires an ORDER BY clause syntactically. You cannot use it without one. SELECT NULL is a hack to shut up the error while not enforcing any particular order. In this case we don't need to enforce any order, so the fastest option is to use SELECT NULL.

    The optimizer sees through this trick, so it has no runtime cost (this claim is easily verified by looking at the execution plan).

    0 讨论(0)
提交回复
热议问题