0

在 SSMS 2014 中,我有一个有效的 SELECT 查询,并希望将其转换为 UPDATE 语句以更新主表中的字段。我已经尝试并尝试了许多解决方案,每个解决方案都会导致不同的错误。

我之前已经设法进行简单的 UPDATE SET WHERE 查询,但是在 WHERE 子句中使用派生表会稍微复杂一些。在搜索时,我找到了一个 JOIN 解决方案,但对我来说,它不喜欢 RIGHT 或 JOIN,在那个解决方案中,SET 是最后一行,SSMS 也不喜欢这样,所以我现在有点困惑。

这是我选择正确 SKU 的工作 SELECT 语句:

SELECT        
    dbo.tblHKProducts.SKU_ID, dbo.tblHKProducts.SKU_Description, 
    dbo.tblHKProducts.SKU_Scan_Group
FROM
    dbo.tblHKProducts 
RIGHT OUTER JOIN
    (SELECT        
         tblHKProducts_1.SKU_ID, tblHKProducts_1.SKU_Description, 
         dbo.ForceScanByDescriptions.FindDescription
     FROM            
         dbo.tblHKProducts AS tblHKProducts_1 
     RIGHT OUTER JOIN
         dbo.ForceScanByDescriptions ON tblHKProducts_1.SKU_Description = dbo.ForceScanByDescriptions.FindDescription
     WHERE        
         (tblHKProducts_1.SKU_Description IS NOT NULL)
    ) AS derivedtbl_1 ON dbo.tblHKProducts.SKU_ID = derivedtbl_1.SKU_ID

我正在尝试做这样的事情,SKU_Scan_Group 是一个字段tblHKProducts

UPDATE dbo.tblHKProducts
SET SKU_Scan_Group = N'Yes'
RIGHT OUTER JOIN
    (SELECT        
         tblHKProducts_1.SKU_ID, tblHKProducts_1.SKU_Description, 
         dbo.ForceScanByDescriptions.FindDescription
     FROM            
         dbo.tblHKProducts AS tblHKProducts_1 
     RIGHT OUTER JOIN
         dbo.ForceScanByDescriptions ON tblHKProducts_1.SKU_Description = dbo.ForceScanByDescriptions.FindDescription
     WHERE        
         (tblHKProducts_1.SKU_Description IS NOT NULL)
     ) AS derivedtbl_1 ON dbo.tblHKProducts.SKU_ID = derivedtbl_1.SKU_ID

任何帮助,将不胜感激!

谢谢凯夫

4

1 回答 1

0

这其实很简单。这应该有效:

UPDATE P
SET P.SKU_Scan_Group = N'Yes'
-- SELECT P.SKU_ID, P.SKU_Description, P.SKU_Scan_Group
FROM dbo.tblHKProducts AS P
RIGHT JOIN (
    SELECT P1.SKU_ID
        , P1.SKU_Description
        , FSBD.FindDescription
    FROM dbo.tblHKProducts AS P1
    RIGHT JOIN dbo.ForceScanByDescriptions AS FSBD
        ON P1.SKU_Description = FSBD.FindDescription
    WHERE P1.SKU_Description IS NOT NULL
    ) AS derivedtbl_1
    ON P.SKU_ID = derivedtbl_1.SKU_ID;

PS 开始使用别名来引用表,您的代码以后可能会变得很难维护。

于 2015-10-10T10:46:46.827 回答