HOW TO UPDATE A PARTICULAR COLUMN USING TRIGGERS

  • I HAVE A TABLE EMP WITH COLUMNS EMPNO,ENAME,SALARY and also a column MOD_DATE

    WHEN EVER I UPDATE SALARY COLUMN I WANT TO

    MODIFY MOD_DATE COLUMN OF SAME ROW WITH THE CURRENT DATE AND TIME.How can i do this by using triggers.

    Any one help would be highly appreciated.

  • First you have to decide what constitutes an update. If a user updates the column to the same value the Update() syntax will still be true. A better choice is to compare the inserted/deleted table values to see if truly has changed. If it has, just update the date column using an update statement.

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

  • CREATE TRIGGER [tg_Emp_Up] ON dbo.Emp

    FOR UPDATE

    AS

    update Emp

    set Mod_Date = getdate()

    from Emp e

    inner join Inserted i on e.EmpNo = i.EmpNo

    inner join Deleted d on i.EmpNo = d.EmpNo

    where i.Salary != d.Salary

    Regards,

    Andy Jones

    andyj93@hotmail.com

    .

  • Thanks you all guys for your help.

    It works great now.

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

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