Triggers

  • Hi All,

    Am new to triggers and my requirement is like:

    I have 2 table User and User_History. Whenever any row

    from User is going to get updated I want to keep a track

    of old row. That is before updating row in User table I want

    to move that row with old values to user_History table.

    Can anybody please tell me how to do that.

    Eagerly waiting for replies.

    Thanks,

    Rohit.

  • you should use the "deleted" and "inserted" tables in your trigger:

    ALTER TRIGGER [dbo].[USER_UPD_HIST] ON [dbo].[User] FOR UPDATE

    AS

    BEGIN

    INSERT INTO User_History(ID, Usermodified, etc.)

    SELECT ID, Usermodified, etc.

    FROM deleted

    END

  • Hi,

    Thank you so much its working fine for update command.

    even I tried the same for delete command also and for that

    one also its working as expected.

    But I didnt get one thing is when to use Inserted and Deleted

    and how does it works I relly didnt get it. I saw that there we

    have written that Select * From Deleted, whats this exactly?

  • In a trigger you can select the data before the change from the "deleted" table, and the data afther the change in de "inserted" table (well, it isn't realy a table).

    When you put the trigger also on the insert, you'll see that only "inserted" has values (since there are no old values". When you do a delete, only the "deleted" is filled, because there are no new values.

  • Do you have any link which has article explaining all these things?

    If so can you please post.

    Am new to Triggers.

    Thanks,

    Rohit.

  • no sorry, just google it (that is what i always do 😉 )

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

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