How to write the trigger to update 2 tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gurujothi D
    New Member
    • Feb 2012
    • 23

    How to write the trigger to update 2 tables

    Dear all,
    I have 2 tables,

    1.lv_data,
    it has the following fields,
    emp_name tot_days
    guru 18



    2.leave_data
    it has the following fields,
    emp_name From_date to_date no_of_days remaining_days
    guru 02/05/2012 03/05/2012 2

    In second table if the data is inserted ,
    the no_of_days will be automatically calculated (from to_date - From_date)+1,

    Here I need to write the trigger to update the remaining_days column,

    In first table for all emp_name tot_days is 18 days,
    so in second table whenever the record is inserted,
    the remaining_days should be calculated like this,

    remaining_days := tot_days - no_of_days and this(calculated ) value should be updated in tot_days column in first table(lv_data),

    Sample Example:
    Code:
    emp_name tot_days
    guru       18

    2.leave_data
    Code:
    emp_name   From_date   to_date   no_of_days   remaining_days
    guru       02/05/2012  03/05/2012    2            16
    Now the first table should be updated like,
    Code:
    emp_name tot_days
    guru       16
    So I need to update 2 tables,
    can someone help me to update these 2 tables through TRIGGER?

    Thank you,
    Regards,
    gurujothi
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    I think you should not store 'no_of_days' in your second table, but simply calculate it when needed.

    Comment

    Working...