Re-indexing table; update with from

Posted by David Thorisson on Stack Overflow See other posts from Stack Overflow or by David Thorisson
Published on 2010-03-22T22:09:27Z Indexed on 2010/03/22 22:11 UTC
Read the original article Hit count: 346

The query says it all, I can't find out the right syntax without without using a for..next

UPDATE Webtree SET Webtree.Sorting=w2.Sorting
    FROM
    (
        SELECT
                BranchID,
                CASE
                    WHEN @Index>=ROW_NUMBER() OVER(ORDER BY Sorting ASC) THEN ROW_NUMBER() OVER(ORDER BY Sorting ASC)
                    ELSE ROW_NUMBER() OVER(ORDER BY Sorting ASC)+1
                END AS Sorting
            FROM Webtree w2
            WHERE w2.ParentID=@ParentID
    )
    WHERE Webtree.BranchID=w2.BranchID

© Stack Overflow or respective owner

Related posts about update

Related posts about sql-server