将工作 SELECT 查询转换为 UPDATE 查询
Convert working SELECT query into UPDATE query
在 SSMS 2014 中,我有一个有效的 SELECT 查询,我想将其转换为 UPDATE 语句以更新主 table 中的一个字段。我已经尝试了很多解决方案,每个都会导致不同的错误。
我以前设法执行过简单的 UPDATE SET WHERE 查询,但是在 WHERE 子句中使用派生的 tables 有点复杂。在搜索时我找到了一个 JOIN 解决方案,但对我来说它不喜欢 RIGHT 或 JOIN 并且在该解决方案中 SET 是最后一行而 SSMS 也不喜欢它所以我现在有点困惑。
这是我的 SELECT 选择正确 SKU 的声明:
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
如有任何帮助,我们将不胜感激!
谢谢
千伏
其实这个很简单。这应该有效:
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;
P.S.
开始使用别名来引用表,您的代码以后可能会变得很难维护。
在 SSMS 2014 中,我有一个有效的 SELECT 查询,我想将其转换为 UPDATE 语句以更新主 table 中的一个字段。我已经尝试了很多解决方案,每个都会导致不同的错误。
我以前设法执行过简单的 UPDATE SET WHERE 查询,但是在 WHERE 子句中使用派生的 tables 有点复杂。在搜索时我找到了一个 JOIN 解决方案,但对我来说它不喜欢 RIGHT 或 JOIN 并且在该解决方案中 SET 是最后一行而 SSMS 也不喜欢它所以我现在有点困惑。
这是我的 SELECT 选择正确 SKU 的声明:
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
如有任何帮助,我们将不胜感激!
谢谢 千伏
其实这个很简单。这应该有效:
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;
P.S. 开始使用别名来引用表,您的代码以后可能会变得很难维护。