[quoted text, click to view] > WITH t (Name, Seq, NewSeq) AS
> (SELECT Name, Seq, ROW_NUMBER() OVER (ORDER BY Seq, Name DESC)
> FROM Seq)
> UPDATE t SET Seq = NewSeq;
> --
> David Portas, SQL Server MVP
very nice!
Say name is a unique key - It appears the natural order in which rows
are inserted works fine for resolving dups. Originally I thought
duplicate seq entries would not ever happen . This does however expose
another question. I need a way of inserting between rows and need the
inserted row to fall infront of the row I inserted on. It does appear
that inserting with the prior seq reordering results in the desired
effect.
say I've got this
select * from seq order by seq
Name Seq
-------------------- -----------
mom 1
dad 2
brother 3
insert seq (name,seq)values ('billybob',2) (with the hope wedging a
row between dad and brother)
results look like this momentarily (which is of course not the desired
effect):
select * from seq order by seq
Name Seq
-------------------- -----------
mom 1
billybob 2
dad 2
brother 3
so we reorganize with your code:
WITH t (Name, Seq, NewSeq) AS
(SELECT Name, Seq, ROW_NUMBER() OVER (ORDER BY Seq, Name DESC)
FROM Seq)
UPDATE t SET Seq = NewSeq;
and now results now look like this.
Name Seq
-------------------- -----------
mom 1
dad 2
billybob 3
brother 4
Correct me if Im wrong. If rows are inserted with the same seq number,
they will naturally order in sequence of entry right? So on that last
data, If I want to insert something between billybob and dad, I just
insert with billybob's seq minus 1 and reorganize.
If so and it appears so that's Great!
My plan is to use this code to allow the users to reorder and insert
steps (rows) using an asp.net gridview with push up, push down and
insert command buttons.
very nice.