SQL Server Join In Order

后端 未结 3 1697
礼貌的吻别
礼貌的吻别 2021-01-12 07:55

I have 2 string in input for example \'1,5,6\' and \'2,89,9\' with same number of element (3 or plus). Those 2 string i want made a \"ordinate join\" as

1           


        
3条回答
  •  心在旅途
    2021-01-12 08:36

    When dbo.Split() returns the data-set, nothing you do can assign the row_number you want (based on their order in the string) with absolute certainty. SQL never guarantees an ordering without an ORDER BY that actually relates to the data.

    With you trick of using (SELECT 0) to order by you may often get the right values. Probably very often. But this is never guaranteed. Once in a while you will get the wrong order.

    Your best option is to recode dbo.Split() to assign a row_number as the string is parsed. Only then can you know with 100% certainty that the row_number really does correspond to the item's position in the list.

    Then you join them as you suggest, and get the results you want.


    Other than that, the idea does seem fine to me. Though you may wish to consider a FULL OUTER JOIN if one list can be longer than the other.

提交回复
热议问题