I have a table called Product
and its data and table structure something like as follows,
+-----------+-------------+-----------+
| ProductId | ProductName | SortValue |
+-----------+-------------+-----------+
| 1157 | ABC | 7 |
+-----------+-------------+-----------+
| 1156 | DEF | 3 |
+-----------+-------------+-----------+
| 1155 | GHI | 4 |
+-----------+-------------+-----------+
| 1154 | JKL | 2 |
+-----------+-------------+-----------+
| 1153 | MNO | 1 |
+-----------+-------------+-----------+
| 1152 | PQR | 5 |
+-----------+-------------+-----------+
| 1151 | STU | 6 |
+-----------+-------------+-----------+
I have another table called LocationProdut
which is the reference product table and location table, for each location productid
has its own sort value.sample data and table structure its as follows,
+--------------+-------------+-----------+
| fkLocationId | fkProductId | Sortvalue |
+--------------+-------------+-----------+
| 19 | 1157 | 1 |
+--------------+-------------+-----------+
| 19 | 1156 | 2 |
+--------------+-------------+-----------+
| 19 | 1155 | 3 |
+--------------+-------------+-----------+
| 19 | 1154 | 4 |
+--------------+-------------+-----------+
| 19 | 1153 | 5 |
+--------------+-------------+-----------+
| 19 | 1152 | 6 |
+--------------+-------------+-----------+
| 19 | 1151 | 7 |
+--------------+-------------+-----------+
| 20 | 1155 | 3 |
+--------------+-------------+-----------+
| 20 | 1154 | 4 |
+--------------+-------------+-----------+
| 20 | 1153 | 5 |
+--------------+-------------+-----------+
| 20 | 1152 | 6 |
+--------------+-------------+-----------+
| 20 | 1151 | 7 |
+--------------+-------------+-----------+
| 21 | 1155 | 3 |
+--------------+-------------+-----------+
| 21 | 1154 | 4 |
+--------------+-------------+-----------+
| 21 | 1153 | 5 |
+--------------+-------------+-----------+
| 21 | 1152 | 6 |
+--------------+-------------+-----------+
| 21 | 1151 | 7 |
+--------------+-------------+-----------+
Now I need to update SortValue
coulmn in LocationProdut
table, in order to Product
table's SortValue
for only fkLocationId = 19 & 20
Expected output as follows.
+------------+-------------+-----------+
| fkBranchId | fkServiceId | Sortvalue |
+------------+-------------+-----------+
| 19 | 1157 | 7 |
+------------+-------------+-----------+
| 19 | 1156 | 3 |
+------------+-------------+-----------+
| 19 | 1155 | 4 |
+------------+-------------+-----------+
| 19 | 1154 | 2 |
+------------+-------------+-----------+
| 19 | 1153 | 1 |
+------------+-------------+-----------+
| 19 | 1152 | 5 |
+------------+-------------+-----------+
| 19 | 1151 | 6 |
+------------+-------------+-----------+
| 20 | 1155 | 4 |
+------------+-------------+-----------+
| 20 | 1154 | 2 |
+------------+-------------+-----------+
| 20 | 1153 | 1 |
+------------+-------------+-----------+
| 20 | 1152 | 5 |
+------------+-------------+-----------+
| 20 | 1151 | 6 |
+------------+-------------+-----------+
| 21 | 1155 | 3 |
+------------+-------------+-----------+
| 21 | 1154 | 4 |
+------------+-------------+-----------+
| 21 | 1153 | 5 |
+------------+-------------+-----------+
| 21 | 1152 | 6 |
+------------+-------------+-----------+
| 21 | 1151 | 7 |
+------------+-------------+-----------+
You could use a simple inner join
to update the required rows:
UPDATE LP
SET
LP.Sortvalue = P.SortValue
FROM LocationProduct LP
JOIN Product P ON LP.fkProductId = P.ProductId
WHERE fkLocationId IN(19, 20);
Please find the db<>fiddle here .
UPDATE targetTable
SET targetTable.targetColumn = s.sourceColumn
FROM targetTable t
INNER JOIN sourceTable s
ON t.matchingColumn = s.matchingColumn
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.