Click here to Skip to main content
15,915,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing an ERP solution in c# winforms and sql server 2008. Our requirement is that all changes done by a user like for example change of sales price needs to be logged with who has done , when it was done and what was the old / new value.
I have read a lot of solutions done by change tracking by we cannot capture the logged in user details there since we don't use windows authentication.
I have used triggers before but how do we pass the logged in user to the trigger?

What I have tried:

I have tried using change tracking but have issue stated above.
Posted
Updated 9-May-16 23:44pm

1 solution

You can't get the user login details from the sql transaction unless every individual uses a different sql login to the server. By the sound of your question, you don't use individual logins (This is a good thing btw, but not very helpful)

Step 1: You have to log a 'modified by', date and type in your data. You can easily add such a column to an existing table:
SQL: ALTER TABLE Statement[^]
I also include a "created by" in my data, but that would be overkill for you.

Step 2: Create your triggers based on Update and Delete (if applicable).
You don't need an insert trigger. Instead log the "deleted" side of any update trigger. This way your 'log' table will contain the previous values and your actual table has the 'what is was changed to' values.
The same is true for the delete statements. This will log the last value before it was removed from the table.

Step 3 ????
Step 4 Profit (Just kidding :P)


So your data will go through this life-cycle

Insert:
Table                             Log table
[values1],modified-by, date1      null

Update 1
Table                             Log table
[values2],modified-by, date2      [values1],modified-by, date1, 'update'

Update 2 
Table                             Log table
[values3],modified-by, date3      [values1],modified-by, date1, 'update'
                                  [values2],modified-by, date2, 'update'

Delete
Table                             Log table
null                              [values1],modified-by, date1, 'update'
                                  [values2],modified-by, date2, 'update'
                                  [values3],modified-by, date3, 'update'
                                  [values3],deleted-by, datenow, 'delete'



As you can see there are 2 entries for Values3. This is because we must log the original modification that we would normally find in the main Table and we must log the delete action.

I hope that's clear
Let me know if you need any further clarification.
Andy ^_^


 
Share this answer
 
Comments
sujit nath 10-May-16 7:05am    
Thanks for the reply Andy. Yes i feel this is the approach we will need to follow. So now i will have to add the last modified by user to all the master tables. Just hoping the triggers will not slow down when reading in import files or bulk inserting data in to the tables
Andy Lanng 10-May-16 8:40am    
That entirely depends on how dense your data is, but there is no way around it :S
best of luck ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900