In my database, I have a few columns whose changes are to be tracked. Let us have A, B and C columns. User and administrator can change the values in these columns. If a cell value(column, row) is changed by administrator, user should not able to change it.
To solve this, I am thinking to maintain a log table for cell changes with fields – Row ID, Column ID, User Changed.
Note : I am using Sql Server 2008, C#.
Please comment on my idea. thanks.
SQL Server does not maintain information about who changed a column last. Therefore you would need to track this information yourself using an INSTEAD OF or AFTER trigger. You would also need to decide what you want to do if a user tries to update Column A and Column B, but the administrator has updated Column A only – do you want to allow the user to update Column B, or should their whole update fail?
You could have an additional column in the table that indicates ChangedByAdmin for each column:
(Using a separate table seems overkill unless you want to maintain the history.)
Now you could have an INSTEAD OF trigger that follows your rules. Just a rough idea off the cuff: