?
Solved

Change data tracking with using triggers (not CDC)

Posted on 2014-01-09
3
Medium Priority
?
629 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 69

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 MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

765 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