使用 union all 更新多列
Update multiple columns using union all
我需要使用另外两个 table 的 union all 从一个 table 更新多个列。
这是一个例子:
UPDATE CustomerTrans
SET SumQuantity = (SELECT SUM(x.Quantity)
FROM
(SELECT *
FROM InventoryTrans
UNION ALL
SELECT *
FROM InventoryTranstemp) x
JOIN
CustomerTrans a ON a.TrnDocumentID = x.TrnDocumentID),
SumPrice = (SELECT SUM(x.Price)
FROM
(SELECT *
FROM InventoryTrans
UNION ALL
SELECT *
FROM InventoryTranstemp) x
JOIN
CustomerTrans a ON a.TrnDocumentID = x.TrnDocumentID)
如您所见,两次选择完全相同。我怎样才能避免它?
接受的答案也使用 join 而不是 union
UPDATE CustomerTrans
SET SumQuantity = (SELECT SUM(x.Quantity),
SumPrice = (SELECT SUM(x.Price))
FROM
(SELECT *
FROM InventoryTrans
UNION ALL
SELECT *
FROM InventoryTranstemp) x
JOIN
CustomerTrans a ON a.TrnDocumentID = x.TrnDocumentID)
使用此语句时出现错误:
Msg 116, Level 16, State 1, Line 11
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
with x as
(
select * from InventoryTrans
UNION ALL
Select * from InventoryTranstemp
)
update CustomerTrans set
SumQuantity= (select sum (x.Quantity) from x
where CustomerTrans.TrnDocumentID=x.TrnDocumentID),
sumPrice= (select sum (x.sumPrice) from x
where CustomerTrans.TrnDocumentID=x.TrnDocumentID)
试试这个:
with tmp as
(
select f1.TrnDocumentID, f1.Quantity, f1.Price from InventoryTrans f1 inner join CustomerTrans f2 on f1.TrnDocumentID=f2.TrnDocumentID
UNION ALL
Select f1.TrnDocumentID, f1.Quantity, f1.Price from InventoryTranstemp f1 inner join CustomerTrans f2 on f1.TrnDocumentID=f2.TrnDocumentID
)
update f0
set f0.SumQuantity=sum (f1.Quantity), f0.SumPrice =sum (f1.Price)
from CustomerTrans f0 inner join tmp f1 on f0.TrnDocumentID=f1.TrnDocumentID
我需要使用另外两个 table 的 union all 从一个 table 更新多个列。
这是一个例子:
UPDATE CustomerTrans
SET SumQuantity = (SELECT SUM(x.Quantity)
FROM
(SELECT *
FROM InventoryTrans
UNION ALL
SELECT *
FROM InventoryTranstemp) x
JOIN
CustomerTrans a ON a.TrnDocumentID = x.TrnDocumentID),
SumPrice = (SELECT SUM(x.Price)
FROM
(SELECT *
FROM InventoryTrans
UNION ALL
SELECT *
FROM InventoryTranstemp) x
JOIN
CustomerTrans a ON a.TrnDocumentID = x.TrnDocumentID)
如您所见,两次选择完全相同。我怎样才能避免它?
接受的答案也使用 join 而不是 union
UPDATE CustomerTrans
SET SumQuantity = (SELECT SUM(x.Quantity),
SumPrice = (SELECT SUM(x.Price))
FROM
(SELECT *
FROM InventoryTrans
UNION ALL
SELECT *
FROM InventoryTranstemp) x
JOIN
CustomerTrans a ON a.TrnDocumentID = x.TrnDocumentID)
使用此语句时出现错误:
Msg 116, Level 16, State 1, Line 11
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
with x as
(
select * from InventoryTrans
UNION ALL
Select * from InventoryTranstemp
)
update CustomerTrans set
SumQuantity= (select sum (x.Quantity) from x
where CustomerTrans.TrnDocumentID=x.TrnDocumentID),
sumPrice= (select sum (x.sumPrice) from x
where CustomerTrans.TrnDocumentID=x.TrnDocumentID)
试试这个:
with tmp as
(
select f1.TrnDocumentID, f1.Quantity, f1.Price from InventoryTrans f1 inner join CustomerTrans f2 on f1.TrnDocumentID=f2.TrnDocumentID
UNION ALL
Select f1.TrnDocumentID, f1.Quantity, f1.Price from InventoryTranstemp f1 inner join CustomerTrans f2 on f1.TrnDocumentID=f2.TrnDocumentID
)
update f0
set f0.SumQuantity=sum (f1.Quantity), f0.SumPrice =sum (f1.Price)
from CustomerTrans f0 inner join tmp f1 on f0.TrnDocumentID=f1.TrnDocumentID