delimiter //
create trigger calcularSubtotal
after update on detalle_pedido for each row
begin
declare total int;
set total= (select sum(SubTotal) from pedidos join detalle_pedido on Pedido=ID_Pedido where ID_Pedido=new.Pedido and ID_Pedido=ID_Pedido);
update Pedidos
set Total = total where ID_Pedido=ID_Pedido and ID_Pedido=new.Pedido ;
end//
Asked
Active
Viewed 30 times
0

Barmar
- 741,623
- 53
- 500
- 612
-
`ID_Pedido=ID_Pedido` is always true, there's no need for it in the `WHERE` clause. – Barmar Sep 21 '22 at 23:44
-
All the examples I can find of this trigger say it happens if you try to update the same table that the trigger is attached to. But you're updating `Pedidos` when the trigger is on `detalle_pedido`. Is that the whole error message? – Barmar Sep 21 '22 at 23:49
-
How are you updating `detalle_pedido`? If that query is using `Pedidos`, the table is locked and you can't update it at the same time in the trigger. See https://stackoverflow.com/questions/41537750/1442-cant-update-table-in-stored-function-trigger-because-it-is-already – Barmar Sep 21 '22 at 23:51
-
https://xyproblem.info/ Post the task itself. – Akina Sep 22 '22 at 04:32