You need to give proper two-part column references.
As it stands, the DB has no idea that the ParentCategoryId
in the sub-query refers to the outer column.
UPDATE c
SET ParentCategoryId = (
SELECT c2.id
FROM Category AS c2
WHERE c2.OldId = c.ParentCategoryId
)
FROM Category AS c
WHERE c.OldId IS NOT NULL;
You can also do this as a joined update:
UPDATE c
SET ParentCategoryId = c2.id
FROM Category AS c
JOIN Category AS c2 ON c2.OldId = c.ParentCategoryId;
-- WHERE c.OldId IS NOT NULL; -- not necessary as now joined
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…