FkLocationId

问题描述 投票:0回答:1

我有一个表叫 LocationProduct 其结构如下:

+--------------+-------------+-----------+
| FkLocationId | FkProductId | SortValue |
+--------------+-------------+-----------+
| 1            | 100         | 1         |
+--------------+-------------+-----------+
| 1            | 101         | 2         |
+--------------+-------------+-----------+
| 1            | 102         | 3         |
+--------------+-------------+-----------+
| 1            | 103         | 4         |
+--------------+-------------+-----------+
| 1            | 104         | 5         |
+--------------+-------------+-----------+
| 1            | 105         | 6         |
+--------------+-------------+-----------+
| 2            | 100         | 1         |
+--------------+-------------+-----------+
| 2            | 101         | 2         |
+--------------+-------------+-----------+
| 2            | 102         | 3         |
+--------------+-------------+-----------+
| 2            | 103         | 4         |
+--------------+-------------+-----------+
| 2            | 104         | 5         |
+--------------+-------------+-----------+
| 2            | 105         | 6         |
+--------------+-------------+-----------+

现在我需要增加 SortValue 当我插入一条新的记录时,将列的值增加一个。举个例子:

INSERT INTO BranchServices(FkBranchId,FkServiceId,SortValue)
VALUES(1,106,1)

我需要更新 FkLocationId = 1 相关的SortValue逐一输入,我需要如下的输出。

+--------------+-------------+-----------+
| FkLocationId | FkProductId | SortValue |
+--------------+-------------+-----------+
| 1            | 100         | 2         |
+--------------+-------------+-----------+
| 1            | 101         | 3         |
+--------------+-------------+-----------+
| 1            | 102         | 4         |
+--------------+-------------+-----------+
| 1            | 103         | 5         |
+--------------+-------------+-----------+
| 1            | 104         | 6         |
+--------------+-------------+-----------+
| 1            | 105         | 7         |
+--------------+-------------+-----------+
| 1            | 106         | 1         |
+--------------+-------------+-----------+

在这里你可以看到我更新了每个旧记录的SortValue。我怎样才能做到这一点?

sql-server sql-update sql-insert
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.