Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
bgalok
Starting Member
2 Posts |
Posted - 2008-11-06 : 20:37:33
|
I am looking to use triggers and/or stored procedures to capture a change history on all tables in a specific database.To do this I planned to use 2 tables: Change_History_Header Change_History_DetailI would like the Header Table to contain ID,(Altered Tables)TableName,(Altered Tables)Primary Key, Timestamp, UserName,The Detailed Table would then contain HeaderID,LineID,(Altered Column)Column_Name, old_value, new_value.So if INSERT to tableA:ID FirstName LastName Email100 Brian Galok Brian.Galok@aol.comChange_HeaderID Table PrimaryKey timestamp UserName123 tableA 100 11/6/2008 8:10:35 PM DOMAIN\userChange DetailHeaderID LineID Column_Name old_value new_value123 01 ID 100123 02 FirstName Brian123 03 LastName Galok123 04 Email Brian.Galok@aol.comThen tableA record 100 is UPDATED TOID FirstName LastName Email100 Brian Galok Brian.Galok@hotmail.comChange_HeaderID Table PrimaryKey timestamp UserName123 tableA 100 11/6/2008 8:30:15 PM DOMAIN\user124 tableA 100 11/6/2008 8:33:01 PM DOMAIN\user2Change DetailHeaderID LID Column_Name old_value new_value123 01 ID 100123 02 FirstName Brian123 03 LastName Galok123 04 Email Bgalok@aol.com 124 01 Email Bgalok@aol.com Brian.Galok@hotmail.comThis should also work for INSERTS and UPDATES on all tablesDelete a record will be handled separately by moving information to an archived table.Can anyone point me in the right direction.I started by creating a trigger on a table to populate the Header on an update but i would also i need it to work on insert:----------ALTER TRIGGER Copy_Cust_w_trigger_updateON dbo.Copy_Cust_w_triggerFOR updateAS insert into Change_History_HeaderSelect 'Customer', i.Customer_ID, getDate(), SUSER_SNAME() From inserted i inner join deleted d on i.Customer_ID = d.Customer_ID----------And After the Header table is populated to detail would need to be populated but I'm not sure how I can pull out the only the fieldnames that have been edited. But I know I can use the insert and delete tables to get the new and the old values once i have field name(s).Brian |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-07 : 01:07:37
|
Why are you hardcoding table names also? i feel you should be logging each tables changes to seperate tables.Just make the audit/history table for those whom you want to track changes.Create a trigger on each to log changes. |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
|
bgalok
Starting Member
2 Posts |
Posted - 2008-11-07 : 11:09:19
|
I planned to use a trigger on each table that i want to audit changes for and since the trigger is unique to the table i hard-coded the table name.I could create a separate audit table for each table and insert the entire record form the deleted table along with any additional audit information I want to collect but I don't want the entire record just the columns that have changed. Also I wanted to try consolidate all changes into one table and use the combination of table_name and the records primary key to link the changes.Brian |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-07 : 11:14:25
|
so you want to put NULLs for unchanged columns?Didnt understand why you need to consolidate all into a single table? Wont columns of all tables be different? |
|
|
|
|
|
|
|