Table update using Trigger

  • I have table A with three fields.

    Emp_Code Char(3),

    Emp_Name varchar(15),

    Policy_Change_Date datetime GetDate() .

    If any one record changes in table A, i wish to get the system date into the Policy_Change_Date. How will i create this trigger?

  • Basically you need to do an update statement in the trigger that sets your date field = getdate() based on the rows that were modified, something like this:

    update a set policy_change_date=getdate() from a inner join inserted b on a.emp_code=b.emp_code

    Assuming emp_code is primary key.

    Andy

  • hey andy, does this mean that inorder for me to update table A, i have to create another table "inserted B" ? is there no other work around to this situation? could you please give me more details?

  • Satish,

    You do not have to create the "inserted" table. SQL Server creates this temporary table itself to handle the data that pertains to your inserted trigger. You can reference this table by its system name of "inserted". Andy has aliased it out here as "B".

  • Thanks Andy and Sean, the trigger worked like a charm !

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply