SQL, Auxiliary table of numbers

前端 未结 7 2017
自闭症患者
自闭症患者 2020-11-21 22:24

For certain types of sql queries, an auxiliary table of numbers can be very useful. It may be created as a table with as many rows as you need for a particular task or as a

7条回答
  •  清酒与你
    2020-11-21 22:57

    This article gives 14 different possible solutions with discussion of each. The important point is that:

    suggestions regarding efficiency and performance are often subjective. Regardless of how a query is being used, the physical implementation determines the efficiency of a query. Therefore, rather than relying on biased guidelines, it is imperative that you test the query and determine which one performs better.

    I personally liked:

    WITH Nbrs ( n ) AS (
        SELECT 1 UNION ALL
        SELECT 1 + n FROM Nbrs WHERE n < 500 )
    SELECT n FROM Nbrs
    OPTION ( MAXRECURSION 500 )
    

提交回复
热议问题