SIGN UP MEMBER LOGIN:    
ARTICLE

Automatic History Using Triggers

Posted by Jean Paul Articles | SQL Server 2012 February 07, 2011
Here I will explain a problem I am facing in Automatic History using Triggers.
Reader Level:
Download Files:
 


I would like to take a moment to explain the following problem I am facing:

I do have a table named Equity which contains the company name and the current PE Ratio.

trigger1.gif

The PE Ratio will be updated daily or weekly based on the wish of user through a web application in ASP.NET. When the process runs for many years, there will be many updates happening to the same record.

Problem

Our problem is to get the historical PE Ratio values, so that we can find out the maximum and minimum values for decision making.

High Cost Solution

One of the solutions we might prefer is that while updating the record from our web application, keep a copy of the record and insert it into another table.

This involves changing the associated business logic layer in C# or changing the stored procedure. I would say this is more time consuming and is a lot of code we have to write to manage the new table if schema changes are involved.

Simple Solution

We can use database triggers to achieve the same. The solution steps are as follows. Believe me, this will take only 5 minutes to do.

  1. Create a new History Table with same structure
  2. Create a Trigger for UPDATE operation

As our History table is not involved in any business logic currently, we can use the Trigger to solve the problem and move on.

Pre-requisites

First, we have to create our original Equity table. The script is given below. The script was generated from Sql Server 2005.

CREATE TABLE [dbo].[Equity](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PERatio] [float] NULL,
[ModifiedOn] [datetime] NULL,
CONSTRAINT [PK_Equity] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Create the History Table

We can create the EquityHistory table using the following script. Please note that there is an additional column HistoryId which keeps track of the History record as there will be multiple history records for each original table record.

CREATE TABLE [dbo].[EquityHistory](
[HistoryId] [int] IDENTITY(1,1) NOT NULL,
[Id] [int],
[Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PERatio] [float] NULL,
[ModifiedOn] [datetime] NULL,
CONSTRAINT [PK_EquityHistory] PRIMARY KEY CLUSTERED
(
[HistoryId] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Create the Trigger

The following Trigger script will do all the magic involved. It says that create a trigger on table Equity whenever UPDATE statement is used. The last line says to get the record from the inserted table and insert the record into the EquityHistory table.

CREATE TRIGGER Equity_Update_Trigger
ON Equity
FOR UPDATE
AS
INSERT INTO EquityHistory SELECT * FROM inserted

The table inserted is a pre-defined table insided triggers. It will give the current record structure while using with INSERT/UPDATE triggers.

Testing the Whole

You can try inserting a new record to the original Equity table. The corresponding record will get automatically inserted in the EquityHistory table.
Now, try changing the PERatio from the first able. You can see a new record gets added to the history table.

Attachment

The attachment file contains queries to create table, insert data, select data. You can try running the file against an sql server database.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor