在SQL 2008中,我使用了这个简单但又不好写的sp:
ALTER PROCEDURE [dbo].[paActualizaCapacidadesDeZonas]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @IdArticulo AS INT
DECLARE @ZonaAct AS INT
DECLARE @Suma AS INT
UPDATE CapacidadesZonas SET Ocupado=0
DECLARE csrSumas CURSOR FOR
SELECT AT.IdArticulo, T.NumZona, SUM(AT.Cantidad)
FROM ArticulosTickets AT
INNER JOIN Tickets T ON AT.IdTicket = T.IdTicket
GROUP BY AT.IdArticulo, T.NumZona
OPEN csrSumas
FETCH NEXT FROM csrSumas INTO @IdArticulo, @ZonaAct, @Suma
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE CapacidadesZonas SET Ocupado = @Suma
WHERE NumZona = @ZonaAct AND IdArticulo = @IdArticulo
FETCH NEXT FROM csrSumas INTO @IdArticulo, @ZonaAct, @Suma
END
CLOSE csrSumas
DEALLOCATE csrSumas
END
我知道:我必须避免使用游标,所以我很确定它可以以适当的方式完成。
我尝试过一次更新查询:
UPDATE CapacidadesZonas SET Ocupado =
(SELECT SUM(AT.Cantidad)
FROM ArticulosTickets AT
INNER JOIN Tickets T ON AT.IdTicket = T.IdTicket
GROUP BY AT.IdArticulo, T.NumZona)
但这确实是错误的,因为select会返回多行。
我对此感觉很糟糕,因为我觉得这对我来说很容易,但我找不到相应的查询。
有什么建议吗?
提前致谢。
答案 0 :(得分:3)
此问题有许多不同的解决方案 - 有关一些选项,请参阅this article。这是一种方法:使用派生表。
UPDATE CapacidadesZonas SET Ocupado=0 WHERE Ocupado <> 0;
UPDATE CapacidadesZonas
SET Ocupado = SUM(s.Cantidad)
FROM CapacidadesZonas C INNER JOIN
(
SELECT T.NumZona, AT.IdArticulo, SUM(AT.Cantidad) as Ocupado
FROM ArticulosTickets AT
INNER JOIN Tickets T ON AT.IdTicket = T.IdTicket
GROUP BY AT.IdArticulo, T.NumZona
) s ON s.NumZona = C.NumZona AND s.IdArticulo = C.IdArticulo;
警告:
答案 1 :(得分:1)
尝试:
UPDATE cz
SET Ocupado = SUM(AT.Cantidad)
FROM CapacidadesZonas as cz
INNER JOIN ArticulosTickets AT ON cz.numZona = at.numZona and cz.IDArticulo = at.IDArticulo
INNER JOIN Tickets T ON AT.IdTicket = T.IdTicket
GROUP BY AT.IdArticulo, T.NumZona