如何在Sql中的表行之间切换两个值

时间:2016-07-29 09:00:43

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2012

我有一个表名[NavBar],其中包含以下列:

Id [int]
Name [nvarchar]
DisplayOrder [int]

示例数据:

Id   Name   DisplayOrder
---------------------------
 1   Home        1
 2   Products    2
 3   Contact     3
 4   Career      4

我想编写一个查询,以便在displayOrder是给定行的下一个更高显示顺序值的两行之间更新DisplayOrder

例如Contact = 4, Career = 3

预期产出:

1  Home      1
2  Products  2
3  Contact   4
4  Career    3

如何执行更新操作以在两行之间切换显示两个并发行的顺序(按Displayorder排序)?

输入参数:仅限一行的Id

4 个答案:

答案 0 :(得分:1)

尝试这样的事情:

UPDATE NavBar  
SET     DisplayOrder =  CASE  
                        WHEN DisplayName  = @row1 THEN (select DipslayOrder from NavBar where DisplayName = @row2)  
                        WHEN DisplayName  = @row2 THEN (select DipslayOrder from NavBar where DisplayName = @row1) 
                        END 
WHERE   DisplayName IN (@row1, @row2);

希望这适合你。

答案 1 :(得分:1)

您需要从输入和下一个记录中获取Displayorder 比你可以更新所有。 它有点复杂但我测试它有效。
此外,当DisplayOrder中存在间隙时,您可以避免麻烦。

declare @InputID int = 3
declare @DisplayOrderInput int
declare @ID int
declare @DisplayOrder int

select @DisplayOrderInput = DisplayOrder from NavBar where ID = @InputID

select top 1
       @ID = ID,
       @DisplayOrder = DisplayOrder
from   NavBar
where  DisplayOrder > (select DisplayOrder from NavBar where ID = @InputID)
order by DisplayOrder

update NavBar set DisplayOrder = @DisplayOrder where ID = @InputID
update NavBar set DisplayOrder = @DisplayOrderInput where ID = @ID

select * from NavBar

答案 2 :(得分:1)

    Try with this.. For any id it will work :

DECLARE @INPUT INT=3 

UPDATE A 
SET    A.DISPLAYORDER = B.DISPLAYORDER 
FROM   #TEMP A 
       INNER JOIN (SELECT ID, 
                          NAME, 
                          CASE 
                            WHEN ID = @INPUT THEN DISPLAYORDER + 1 
                            WHEN ID = @INPUT + 1 THEN DISPLAYORDER - 1 
                            ELSE DISPLAYORDER 
                          END DISPLAYORDER 
                   FROM   #TEMP)B 
               ON A.ID = B.ID    


    Output :

    ID  NAME    DISPLAYORDER 
    1   Home        1
    2   Products    2
    3   Contact     4
    4   Career      3

答案 3 :(得分:1)

试试这个 -

-- 1) Get Current row's displayorder and Next Row's id in Temp table
SELECT nb1.DisplayOrder, nb2.id 
into #TempNavBar
FROM [NavBar] nb1
INNER JOIN [NavBar] nb2 ON nb1.id = nb2.id - 1
WHERE nb1.Id = 3

-- 2) Update Actual table by using Self join on Next row
UPDATE nb1
    SET nb1.DisplayOrder = nb2.DisplayOrder
FROM [NavBar] AS nb1
INNER JOIN [NavBar] AS nb2 ON nb1.id = nb2.id - 1
WHERE nb1.Id = 3

-- 3). Update Actual table by joining it with temp table
UPDATE nb1
    SET nb1.DisplayOrder = tmp2.DisplayOrder
FROM [NavBar] AS nb1
INNER JOIN #TempNavBar AS tmp2 ON nb1.id = tmp2.id

-- Cleanup operation
DROP TABLE #TempNavBar

我在这里做的是加入NavBar表,但是在下一行id上,获取当前行的DisplayOrder列和下一行的id列值并将其插入temp表

在第二个查询中,我更新了当前行displayOrder值的值及其下一行的值。

在第三个查询中,我通过将[NavBar]表与临时表连接,用临时表displayOrder值更新DisplayOrder值。

<强>结果

Id  Name    DisplayOrder
------------------------
1   Home        1
2   Products    2
3   Contact     4
4   Career      3