Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 28,428 times

Contents

Related Categories

Using Triggers to track database action history - Create triggers

tlkelley

Create triggers

At this point, we will create our trigger to automatically insert a record into the History_Account_Info whenever an update or insert is made to the Account_Info table. I will demonstrate how to create the triggers using SQL Server 7 Enterprise Manager. Start by right-clicking the Account_Info table and selecting "All Tasks" and then "Manager Triggers...".

At this point, the Trigger Properties dialog box then appears. This is where you enter your Trigger code. Let's first create an insert trigger, one that will fire when a record is inserted into the Account_Info table.

CREATE TRIGGER [insert_history] ON Account_Info
FOR INSERT
AS
INSERT History_Account_Info (history_account_id, history_account_name,
                            history_account_balance, modifier,
                            modified_date, action)
SELECT account_id, account_name, account_balance,
      modifier, GETDATE(), 'INSERTED'
FROM inserted

Now click the Check Syntax button to verify that you have typed everything correctly, then click OK. Our last trigger that we will create will be an update trigger, one that fires when a record in the Account_Info table is updated.

CREATE TRIGGER [update_history] ON Account_Info
FOR UPDATE
AS
INSERT History_Account_Info (history_account_id, history_account_name,
                            history_account_balance, modifier,
                            modified_date, action)
SELECT account_id, account_name, account_balance,
      modifier, GETDATE(), 'BEFORE UPDATE'
FROM deleted

INSERT History_Account_Info (history_account_id, history_account_name,
                            history_account_balance, modifier,
                            modified_date, action)
SELECT account_id, account_name, account_balance,
      modifier, GETDATE(), 'AFTER UPDATE'
FROM inserted

Well, that is it! Now simply use your application and a history is recorded of all your changes. Or simply insert or update directly through SQL Server 7. Hope this helps some of you!

Always working hard!!

Comments

  • Re: Bulk Operations

    Posted by Jeff Moden on 13 Feb 2008

    Oh, no.... I didn't say anything about using a cursor.  There's no need for the RBAR (pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row").  Do it...

  • Bulk Operations

    Posted by sdietze on 14 Jan 2005

    Hi Tal,

    Jeff is right. Just declare a cursor that iterates over the inserted/deleted table. It should fetch at least your PKs, which you can use to retrieve the inserted/deleted lines - one after a...

  • You CAN do mass updates of audit table w/ trigger

    Posted by Jeff Moden on 12 May 2004

    Hi Tal,

    You wrote "Note: A database trigger only fires once per statement; therefore, if you perform a mass update or mass delete then those instances will not be recorded for each record. Keep th...