1

我在表中有一个“SortIndex”列,我想添加一个“案例更新”以通过传入 ID 和所选表的新位置来重新排序该表。

SQL小提琴

DECLARE @T TABLE
(
    ID int,
    Name char(1),
    SortIndex int
)

INSERT @T 
SELECT 21, 'A', 1 UNION ALL
SELECT 23, 'B', 2 UNION ALL
SELECT 35, 'C', 3 UNION ALL
SELECT 45, 'D', 4 UNION ALL
SELECT 55, 'E', 5

SELECT * FROM @T    

DECLARE @SortIndex int

/* MOVE A -> 4 */

SET @SortIndex = (SELECT SortIndex FROM @T WHERE ID = 23)

/*
UPDATE @T
    SET 
        SortIndex = CASE ... ??
*/      

/* MOVE D -> 2 */

SELECT * FROM @T

这是否可以通过仅使用 case 语句来重建 sortindex 来解决这个问题?

更新

期望的结果

A -> 4

    Name SortIndex
    B     1 
    C     2
    D     3
    A     4
    E     5

    D -> 2

    A     1 
    D     2
    B     3
    C     4
    E     5
4

2 回答 2

2

使用以下代码移动和重建索引,它在所需元素之后添加元素并移动其他元素(根据注释说明回答):

这是更苗条的版本:

 DECLARE @SortIndex int,@MoveTo int, @CurrentSortIndex int

SET @CurrentSortIndex  = (SELECT SortIndex FROM @T WHERE ID = 45) --D
SET @MoveTo = 2

UPDATE--D
  @T
SET
  SortIndex = @MoveTo
WHERE
  ID = 45

UPDATE
  @T
SET
  SortIndex = SortIndex - ((@MoveTo-@CurrentSortIndex)/(ABS(@MoveTo-@CurrentSortIndex)))
WHERE
(
    SortIndex BETWEEN @CurrentSortIndex AND @MoveTo
 OR
    SortIndex BETWEEN @MoveTo AND @CurrentSortIndex  
)
AND
  ID != 45

将 A 移动到位置 2 的完整示例,也是 sqlfiddle 链接http://sqlfiddle.com/#!3/d41d8/5918

DECLARE @T TABLE
(
    ID int,
    Name char(1),
    SortIndex int
)

INSERT @T 
SELECT 21, 'A', 1 UNION ALL
SELECT 23, 'B', 2 UNION ALL
SELECT 35, 'C', 3 UNION ALL
SELECT 45, 'D', 4 UNION ALL
SELECT 55, 'E', 5


DECLARE @SortIndex int,@MoveTo int, @CurrentSortIndex int
DECLARE @IDToMove INT 
SET @IDToMove = 21--A

SET @CurrentSortIndex  = (SELECT SortIndex FROM @T WHERE ID = @IDToMove) 
SET @MoveTo = 2

UPDATE
  @T
SET
  SortIndex = @MoveTo
WHERE
  ID = @IDToMove

UPDATE
  @T
SET
  SortIndex = SortIndex - ((@MoveTo-@CurrentSortIndex)/(ABS(@MoveTo-@CurrentSortIndex)))
WHERE
(
    SortIndex BETWEEN @CurrentSortIndex AND @MoveTo
 OR
    SortIndex BETWEEN @MoveTo AND @CurrentSortIndex  
)
AND
  ID != @IDToMove

SELECT * FROM @T ORDER BY SortIndex

初始版本:

DECLARE @SortIndex int,@MoveTo int, @CurrentSortIndex int

SET @CurrentSortIndex  = (SELECT SortIndex FROM @T WHERE ID = 21) --A
SET @MoveTo = 2

UPDATE--A
  @T
SET
  SortIndex = @MoveTo
WHERE
  ID = 21

IF @MoveTo > @CurrentSortIndex

UPDATE
  @T
SET
  SortIndex = SortIndex - 1
WHERE
  SortIndex BETWEEN @CurrentSortIndex AND @MoveTo
AND
  ID != 21

 IF @MoveTo < @CurrentSortIndex

UPDATE
  @T
SET
  SortIndex = SortIndex + 1
WHERE
  SortIndex BETWEEN @MoveTo AND @CurrentSortIndex
AND
  ID != 21
于 2012-11-06T15:56:21.607 回答
0
UPDATE @T
SET SortIndex = CASE Name 
        when 'A' then 4 
        when 'D' then 2 
        else Name 
    end
于 2012-11-06T15:27:24.493 回答