In conjunction with the fn_split function, I’m returning a list of results from a table based on comma separated values.
The Stored Procedure T-SQL is as follows:
SELECT ProductCode, ProductDesc, ImageAsset, PriceEuros, PriceGBP, PriceDollars, replace([FileName],' ','\_') as [filename], ID as FileID, weight from Products LEFT OUTER JOIN Assets on Assets.ID = Products.ImageAsset where ProductCode COLLATE DATABASE_DEFAULT IN (select [value] from fn\_split(@txt,',')) and showOnWeb = 1
I pass in to the @txt param the following (as an example):
ABC001,ABC009,ABC098,ABC877,ABC723
This all works fine, however the results are not returned in any particular order – I need the products returning in the ‘SAME ORDER’ as the input param.
Unfortunately this is a live site with a built schema, so I can’t change anything on it (but I wish I could) – otherwise I would make it more sensible.
If all of the references that are passed in on the @txt param are unique you could use CharIndex to find their position within the param e.g.