I have a SP that updates some rows and I want to update the SortOrder column I have with it’s position in the select – here is part of my code:
UPDATE StagingCategoryItems
SET IsDeleted = 0,
ModifiedOn = GETDATE(),
ModifiedBy = 'DragDropSP'
WHERE ItemID IN (
SELECT ItemID
FROM StagingCategoryItems
WHERE ManufacturerID=@ManufacturerID
AND CategoryID=@CategoryID
AND ItemID IN
(
SELECT items
FROM dbo.Split(@InIds,',')
)
AND IsDeleted=1
)
AND ManufacturerID=@ManufacturerID AND CategoryID=@CategoryID
So I would want to add a:
SortOrder = POSITION_IN_SELECT_RESULT
How would I accomplish this?
If you are talking about the input order in
@InIds, it will likely require changing the function to also include the rank in the output. Assuming a numbers table:You can create a function like this:
So now your
UPDATEcould look something like this:(I also removed your redundant check against the same table.)