UPDATE main
SET main.PCH_2YR =
(SELECT TOP 1 sub.PCH_2YR FROM [dbGlobalPricingMatrix].[dbo].[tblPCHLookup_Test] sub WHERE
sub.capid = main.capid AND
sub.milespa = main.mileage AND
sub.maintained = main.maintenance AND sub.pch_2yr IS NOT NULL)
FROM [dbWebsiteLO3-PRICING]..Vehicles_Staging_Data main
如果子查询返回的值不是NULL
,我只想设置值。我怎么能这样做?
只有一行符合条件,但我只想使用它的值,如果它不是NULL。
答案 0 :(得分:2)
尝试如下查询:
UPDATE main
SET main.PCH_2YR =sub.PCH_2YR
FROM [dbWebsiteLO3-PRICING]..Vehicles_Staging_Data main LEFT JOIN
[dbGlobalPricingMatrix].[dbo].[tblPCHLookup_Test] sub ON
sub.capid = main.capid AND
sub.milespa = main.mileage AND
sub.maintained = main.maintenance AND sub.pch_2yr IS NOT NULL
WHERE sub.PCH_2YR is NOT NULL
答案 1 :(得分:0)
您可以在桌面上执行INNER JOIN
而不是子查询。 INNER JOIN
过滤掉另一个表中没有匹配项的记录。
UPDATE main
SET main.PCH_2YR = sub.PCH_2YR
FROM [dbWebsiteLO3-PRICING]..Vehicles_Staging_Data main
INNER JOIN [dbGlobalPricingMatrix].[dbo].[tblPCHLookup_Test] sub
ON sub.capid = main.capid
AND sub.milespa = main.mileage
AND sub.maintained = main.maintenance
AND sub.pch_2yr IS NOT NULL