For the issue of sending all ShopId's into the procedure at once I suggest using an XML-parameter and then creating a table variable out of it:declare @Shops xmldeclare @tShop table(ShopId uniqueidentifier)set @Shops= '<shops> <shop>49506541-4ce2-40ac-812a-7ab262e6f0b0</shop> <shop>49506541-4ce2-40ac-812a-7ab262e6f0b1</shop> <shop>49506541-4ce2-40ac-812a-7ab262e6f0b2</shop> </shops>'insert into @tShop(ShopId)select T.c.value('.', 'uniqueidentifier') as ShopIdfrom @Shops.nodes('/shops/shop') T(c)You can of course use a comma separated string if you feel more comfortable with that, I doubt you will notice any performance hit with only 27 shop-id's. The important thing is that you end up with a table containing the shop-id's.Then you can join on the shop-table and combine it with the window function dense_rank() to be able to fetch the products you want, something like this:select t.[Rank], RP.*from RawProducts RP inner join ( SELECT iSh.ShopId, iRP.RawProductId, ftt.[Rank], dense_rank() over(partition by iSh.ShopId order by ftt.[Rank] desc) DenseRank FROM @tShop iSh inner join RawProducts iRP on iRP.ShopId = iSh.ShopId INNER JOIN CONTAINSTABLE ( RawProducts, RawProductName, 'ISABOUT("*radox*","*shower*")' ) AS ftt ON ftt.[KEY] = iRP.RawProductId ) t on t.RawProductId = iRP.RawProductId and t.ShopId = iRP.ShopIdwhere t.DenseRank <= 5order by t.ShopId, t.[Rank] descPlease note that I have not been able to test the code, so you will probably face some errors at first, but hopefully you will get the idea.