我正在尝试编写以下更新语句;
UPDATE @eticat
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = @can_cmc_id
GROUP by eticat_id
) as t
WHERE eticat_id = t.eticat_id
但它一直在抱怨暧昧的专栏。有人可以告诉我我做错了什么。
编辑:错误消息是“不明确的列名'eticat_id'。”
该行是'WHERE eticat_id = t.eticat_id'
答案 0 :(得分:2)
首先,那不是CTE;它是一个派生表。相似,但不同:)
其次,您正在更新未包含在FROM子句中的表变量,这会使SQL Server感到困惑。尝试类似:
UPDATE x
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = @can_cmc_id
GROUP by eticat_id
) as t JOIN @eticat x ON x.eticat_id = t.eticat_id