我似乎无法从内部联接方法获取更新。我曾尝试用几种不同的方式来写它无济于事。行受到影响,但产品表的值从未实际更新过。
选择要测试的代码(运行更新查询后不应返回任何结果):
SELECT
[P].[ProductCode],
[P].[PriceSelling],
[ID].[InclPrice],
[P].[StockQty],
[ID].[Qty]
FROM
dbo.Sync_ImportData AS [ID]
INNER JOIN
dbo.Sync_Product AS [P]
ON
[P].[ProductCode] = [ID].[ISBN]
WHERE
([P].[StockQty] <> [ID].[Qty] OR [P].[PriceSelling] <> [ID].[InclPrice])
更新代码:
UPDATE [P]
SET
[P].[StockQty] = [ID].[Qty],
[P].[PriceSelling] = [ID].[InclPrice]
FROM
dbo.Sync_Product AS [P]
INNER JOIN
dbo.Sync_ImportData AS [ID]
ON
[P].[ProductCode] = [ID].[ISBN]
WHERE
([P].[StockQty] <> [ID].[Qty] OR [P].[PriceSelling] <> [ID].[InclPrice])
基本上我需要更新股票数量和价格,如果进口数据a)有产品记录(ISBN匹配ProductCode)和b)任何一个值需要更新(数量或价格与ImportData不同)
答案 0 :(得分:1)
如果表之间存在多个匹配项,则可以看到此行为。特别是,如果ISBN
中的Sync_ImportData
不唯一:
SELECT id.ISBN, COUNT(*)
FROM dbo.Sync_ImportData id
GROUP BY id.ProductCode
HAVING COUNT(*) > 1
如果有重复项,则其中一行将用于更新 - 任意。更改与第二行不匹配。