Solved

Change data tracking with using triggers (not CDC)

Posted on 2014-01-09
3
620 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 350 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 150 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Syntax to get a value associated with most recent date 16 61
SQL 2012 Report Builder 3.0 query 2 18
Complex SQL Server WHERE CLause 9 37
Report 8 24
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

738 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