?
Solved

Change data tracking with using triggers (not CDC)

Posted on 2014-01-09
3
Medium Priority
?
640 Views
Last Modified: 2014-01-22
Hello Folks,

I've to use triggers (insert, delete, update) for change data in SQL Server 2012.
The reason to use triggers as I've Standard edition only and CDC available only in Enterprise edition.

FYI, I am planning to make trigger only each table for DML opeation and trigger will make a log in replica of table (i.e. lets call history table for each) along with a flag which will denote either respective record is insert, update or delete.

Could you please assist on following points?

1. What will be the optimal way to fetch from history table as I need result at GUI with field name, old value, new value, modify by, modified date?

2. What are pros and cons for this approach as I have total around 180 tables?

FYI, the history table might have values which will be dependent on other tables like City Id in main table and for fetch City Name, we need to fetch from CityMaster table (For GUI).

Could you please assist on fetch change data with triggers only as CDC available only in Enterprise edition and I am working on Standard edition.

Best Regards,
Mohit Pandit
0
Comment
Question by:MohitPandit
3 Comments
 
LVL 25

Accepted Solution

by:
jogos earned 1400 total points
ID: 39770326
1.
Because history table will be huge don't expect a performant speedy result.
For having things in right order the row_number() over()  is a function that comes handy. When grouping on table/guid and ordering descending on date rownumber  is the last change
http://technet.microsoft.com/en-us/library/ms186734.aspx 

2.
Trigger's see that they are efficient and can handle statements that insert/update/delete multiple records in one time. Is it worth blocking your app when your logging fails for some reason.... think about that.  Triggers always pain.
Give triggers identifieable names and perhaps a procedure to disable them 'on call' (not if it is a audit-logging for legal reasons).
Don't put indexes on that loggingtable it will slow down every action.  Think about deleting history on regular basis .... that is transfering it from a logging-table to a reporting-table.
That logging table will become a source for blocking transactions, know your data and processes so that maybe multiple logging tables can ease the pain.
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 600 total points
ID: 39772295
Log row by row NOT column by column.

You can capture only the key column(s) and the column(s) that changed if you want, but capture ALL changed columns in ONE row, instead of INSERTing a separate row for each.  Query code can split out the change by column later if that is needed.

For best performance, you'll likely have to use a nonclustered index for the queries, leaving the main table a heap for best insert performance on it.
0
 
LVL 5

Author Closing Comment

by:MohitPandit
ID: 39802010
Thanks
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question