problem element circuit

The essence of the problem: there is a trigger that when entering data, the E1/E3 table should, by Dirt_ID find all the goods in the order and convert its total price ( Price ) to the final amount in the E3 table ( Request_sum ). There is a code, but it summarizes in general all the prices of all the goods available in all orders.

 create trigger ChecksumOrder on E1_Dirt_E3_Order for insert as update E3_Order set Request_sum = (select sum(Price) from E1_Dirt i join E1_Dirt_E3_Order e on i.ID_Dirt=e.ID_Dirt) where E3_Order.ID_Order != E3_Order.ID_Order join E1_Dirt_E3_Order c on c.ID_Order=e.ID_Order go 
  • one
    In sql-server triggers, a special "table" is available, in which new data is introduced by the trigger. It should be used in the request. In general, such triggers usually do not need to summarize anything over the whole set of records. just add to Price the new value given by the added record and subtract the old value of docs.microsoft.com/ru-ru/sql/relational-databases/triggers/… - Mike

0