Various Triggers – Part 4

Another usage of Data Modification Language (DML) triggers is the replicating data for historical tracking or data aggregation.

Today, I am going to expand on the AUTOS database that I created in prior articles to demonstrate such usage. The business unit has asked us to track each macro task in the making of an automobile.

 

To accomplish this goal, I am going to add the following objects to the AUTOS database.

STATUS_CODE – a coded version of the status of a given task.
MAKE_STATUS – for a given vehicle (VIN), what is the current status.
MAKE_HISTORY – all tasks and status for building a vehicle (VIN).
TRG_MAKE_STATUS_TO_HISTORY – a trigger that copies the current insert/update to the history table.

It is assumed that the following UPSERT stored procedure will be used when populating vehicle information.

 

I am going to use the macro tasks for automobile making described in this ehow article. There are really no new TSQL statements being introduced with this example. The typical CREATE, ALTER, and DROP statements are used in the sample code.

To illustrate the trigger working, I am going to show the history table, as defined below, full of data.

 

The resulting data rows after inserting 10 macro steps with one rework action is shown below. It is not a coincident that I used increments of 100 seconds multiplied by the task number as the duration.

 

In summary, after triggers can be used to duplicate data for historical record keeping. This history table might or might not be in the same database. Having the history data in a different database allows for applying techniques such as data compression and read only file groups to reduce administrative overhead.

Related posts

Leave a Comment