Given an example table such as:
CREATE TABLE TESTING_Order(
Order INT,
Name VARCHAR(5)
)
INSERT INTO TESTING_Order
VALUES
(0, 'Zero'),
(1, 'One'),
(2, 'Two'),
(3, 'Three'),
(4, 'Four'),
(5, 'Five'),
(6, 'Six'),
(7, 'Seven')
I'd like to know how to implement a sort of 'Stack' to show, for instance, the turn order in a game where the first player moves 'left' each turn. I can accomplish this using two update statements:
UPDATE TESTING_Order
SET Order = Order + 1
UPDATE TESTING_Order
SET Order = (SELECT Min(Order) -1 FROM TESTING_Order)
WHERE Order = (SELECT MAX(Order) FROM TESTING_Order)
I was wondering if there is a cleaner/more proper way of doing this, and especially if that other way can be done using a single UPDATE statement.
In other words, I think what I'm after is a better implementation of a LIFO Stack that performs a push on every pop--excuse me for possibly butchering any terminology.
... SET Order = (Order + 1) % (SELECT COUNT(*) FROM Table)
? (orMAX() + 1
if you preferred that overCOUNT()
for some reason; either way, it appears to assume that the numbers are all consecutive with no gaps) Regardless, if you execute your multiple statements within a transaction, they'll effectively be executed as one atomic operation, if that's what you're worried about.