1
Considering the following trigger
:
CREATE TRIGGER tgr_vendas_d
ON vendas
FOR DELETE
AS
BEGIN
DECLARE @valor DECIMAL(10,2),
@data DATETIME;
SELECT @data = d.data,
@valor = d.valor
FROM deleted d
UPDATE c
SET saldo_final = saldo_final - @valor
FROM caixa c
WHERE c.data = @data
END
GO
If more than one record is deleted, only the last value is updated. Considering this, I have two questions:
1) Why this happens?
2) How to fix the problem efficiently?
http://www.devmedia.com.br/cursores-no-sqlserver/5590 you will need a Cursor
– Motta
@Motta exactly. It has this shape and one without cursor too. You can add the answer?
– Sorack
@Sorack: When a sale line is deleted, shouldn’t the cash balance be updated for other later dates as well? That is, the one-day cash balance is used as the starting balance the next day or the balance is individually controlled for each day?
– José Diz
@Josédiz yes... This problem is not real, it’s just an example I put to facilitate understanding
– Sorack