Question : ReOrder Priorirty

Dear Friends,

please first check the related question,
here i got the solution for reorder column int value
but in the solution whenever i execute the query its increment all values

ex
Name   Priority
A            1
B            3
C            2
D            4

when i execute the query which is given in the previous solution

say i change the priority of C to 1

then its

Name   Priority
A            2
B            4
C            1
D            5

so its increment everytime whenever i exec the query

what i want when i exec the query its update like this

Name   Priority
A            2
B            3
C            1
D            4

hope u understand my requirement


thanx

Answer : ReOrder Priorirty

Sorry
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
declare @name varchar(10)
declare @newpriority int

set  @name = 'C'
set @newpriority = 1


declare @oldpriority int
select @oldpriority = priority from yourtable where name = @name

UPDATE yourtable
   SET Priority = CASE 
            WHEN Name = @Name then @newpriority
            WHEN @newpriority > @oldpriority THEN priority -1
            WHEN @newpriority < @oldpriority THEN priority +1
            ELSE Priority
   END -- end of case
WHERE ( @newpriority > @oldpriority AND priority <= @newpriority AND priority >= @oldpriority )
    OR  ( @newpriority < @oldpriority AND priority >= @newpriority AND priority <= @oldpriority )
Random Solutions  
 
programming4us programming4us