回答済み Trigger

  • Tuesday, January 09, 2007 4:51 PM
     
     

    hello team:

    i have two tables order and orderdetial, i want from the orderdetail the sum of the quantity of products  for each order and then insert this sum in order table infront of each order

    this is the first part:

    declare @s decimal
    select @s=sum(f.quantity) from orderdetails as f
    inner join orders as a
    on a.orders= f.orders
    group by f.orderdetails

    any one have advise, thanks lot

All Replies

  • Tuesday, January 09, 2007 5:14 PM
    Moderator
     
     

    Hassano,

    It is considered a very bad idea to store data that can be calculated from other data. It becomes 'easy' to have the data desyncronized. Your idea of using a TRIGGER may help offset that concern -but at a significant performance cost.

    The use of a TRIGGER on the [Order Details] table is not the best idea since the TRIGGER code will have to execute every time there is a new row INSERT, a current row UPDATED, or even DELETED. It could cause a lot of unnecessary code execution. Imagine adding an order with ten rows in the [Order Details] table -the TRIGGER code would have to execute ten times.

    A better solution would be to have a VIEW that joins the two tables, and calculates the total of the order. Then the code would only execute when you use the VIEW.

    If you are insistent upon using a TRIGGER, let us know and we will help you with it -even thought it is not a good idea.

  • Tuesday, January 09, 2007 8:40 PM
     
     

    I realy thank you very much , if you mind to help me to wright the code of the view

    Thanks lot

  • Tuesday, January 09, 2007 8:44 PM
     
     

    I realy thank you very much , if you mind to help me to wright the code of the view

    The point is that i want the user to enter values in order detail with out seeing the total in the same form .

    Thanks lot

  • Tuesday, January 09, 2007 8:52 PM
    Moderator
     
     

    In order to help you with the VIEW, we need to know the table definitions. What are the columns and datatypes.

    (Or, by chance, are you using the Northwind database as an example?)

  • Tuesday, January 09, 2007 10:26 PM
     
     

    i am not using the northwind but i found that the nearst example for my case is order and orderdetails to not wright many details to be easier to understand   

    for ex:

    The order table  pk : orderid , orderdate, ordername ,totalquantity  -------  of products 

         orderdetails   fk   : orderid , prodcutname , quantity

    can you learn me the two ways if you mind the trigger and the view , thank you very much

    is it better to make another table like a 'store' to save the totals

    Thanks lot

  • Wednesday, January 10, 2007 3:02 AM
    Moderator
     
     Answered

    The view is easy:

    create view ordersWithTotal
    as
    select orders.orderId,
           --order.columns,
           sum(od.unitPrice * od.quantity) as orderAmount
    from   orders
             join [order details] od
                on orders.orderId = od.orderId
    group by orders.orderId

    The trigger is far less trivial. You have to take into consideration that multiple rows can be updated, so you do something like:

    use northwind
    go
    alter table orders add orderTotal numeric(12,2) null
    go
    update orders
    set    orderTotal = orderTotal.orderAmount
    from   orders
              join (select orders.orderId,
                           sum(od.unitPrice * od.quantity) as orderAmount
                    from   orders
                             join [order details] od
                                on orders.orderId = od.orderId
                    where  orders.orderId in (select orderId from inserted)
                    group by orders.orderId) as orderTotal
            on orders.orderId = orderTotal.orderID
    go

    The bold bit is the most important part of the query as it constrains the rows updated to only those that are modified.  You would create an insert and update trigger to make this work. 

     

     

  • Wednesday, January 10, 2007 4:46 PM
     
     

    Re ask :

       my question is :
       i have master and detail tables , the pk of the master used many times as fk in the detail table, i want to make for ex a trigger to get the sum for a feild of the fk then insert this sum in the master table in feild total
    like productid and store for example
     
    how can i do that VIP
    thank you very much
  • Thursday, January 11, 2007 10:59 AM
     
     
    Hello Team no answer for the re ask
  • Thursday, January 11, 2007 11:06 AM
     
     

    On the Trigger (Insert trigger on OrderDetails)..

    Update Ordermaster

    Set

     Total = TotalValue

    From

      (Select Sum(OrderValue) as [TotalValue] From OrderDetails Where OrderDetails.OrderId = New.OrderId) as Data

    Where

      Ordermaster.OrderId = New.OrderId

  • Thursday, January 11, 2007 7:33 PM
     
     

    Thank you very much the view is accuret Thanks lot

    about the trigger i hava a question

    what is  'set    orderTotal = orderTotal.orderAmount '    <-- i couldnt understand it

    Thank you ver much

  • Thursday, January 11, 2007 7:37 PM
    Moderator
     
     

    Look at the join for the derived table (bold)

    update orders
    set    orderTotal = orderTotal.orderAmount
    from   orders
              join (select orders.orderId,
                           sum(od.unitPrice * od.quantity) as orderAmount
                    from   orders
                             join [order details] od
                                on orders.orderId = od.orderId
                    where  orders.orderId in (select orderId from inserted)
                    group by orders.orderId) as orderTotal
            on orders.orderId = orderTotal.orderID
    go

    This derived table is where I calculated the orderAmount, and the derived table is named orderTotal (in italics).  Derived tables are really great for doing this sort of thing