DECLARE @new_item_priority INT = 1
DECLARE @reorder_priority AS TABLE (item_id INT, item_priority INT )
INSERT @reorder_priority
SELECT item_id, ROW_NUMBER() OVER(ORDER BY rn) AS item_priority
FROM (
SELECT item_id,
CASE
WHEN item_id = @p_item_id AND @new_item_priority < item_priority THEN CAST(@new_item_priority AS FLOAT) -.1
WHEN item_id = @p_item_id AND @new_item_priority > item_priority THEN CAST(@new_item_priority AS FLOAT) +.1
ELSE item_priority
END rn
FROM dbo.items
-- WHERE clause if needed..
) x
ORDER BY rn
UPDATE i
SET item_priority = rp.item_priority
FROM dbo.items i
JOIN @reorder_priority rp ON rp.item_id = i.item_id
Tuesday, January 14, 2014
Set Based way to order items with SQL
so you have a list of rules or something and you want your users to be able to change the processing order
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment