Wrong order in Table valued Function(keep “order” of a recursive CTE)

后端 未结 3 1578
鱼传尺愫
鱼传尺愫 2021-01-21 07:39

a few minutes ago i asked here how to get parent records with a recursive CTE. This works now, but I get the wrong order(backwards, ordered by the PK idData) when i create a Tab

3条回答
  •  Happy的楠姐
    2021-01-21 07:55

    There is no ORDER BY anywhere in sight - neither in the table-valued function, nor in the SELECT from that TVF.

    Any "ordering" will be totally arbitrary and coincidental.

    If you want a specific order, you need to specify an ORDER BY.

    So why can't you just add an ORDER BY to your SELECT:

     SELECT * FROM dbo._previousClaimsByFiData(16177344) 
     ORDER BY (whatever you want to order by)....
    

    or put your ORDER BY into the TVF:

    INSERT INTO @retPreviousClaims
        SELECT idData FROM PreviousClaims
        ORDER BY idData DESC (or whatever it is you want to order by...)
    

提交回复
热议问题