Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Never actually deleting records can provide an extra layer of data protection.

A common approach is to have an IsDeleted flag on each database table to specify whether the record is "deleted" or not. However, without the proper low-level mechanism in place to filter out the records, bugs can begin to invade the application space (and they're not always easy to spot!).

In my case, I have written my own Entity classes to handle CRUD operations. The SELECT methods within these classes automatically filter on IsDeleted and the classes are auto-generated by a tool I wrote to ensure this issue is minimized. However, I recognize some folks may want to use other techniques to address this issue.

What technique(s) do you prefer? Does Microsoft SQL Server provide a low-level mechanism that everything flows through that permits the modification of incoming SELECT statements to add the appropriate filtering prior to the statement being processed?

It'd be awesome if SQL Server provided an object model of the SELECT statement where the reference Tables could be iterated through and you could append an [AND] IsNull(IsDeleted, 0) = 0 expression to each.
Posted
Updated 30-Jun-11 6:13am
v2

1 solution

SQL Server does not provide this functionality out of the box. For a start, it's very much a brute force method where you would end up having to work out whether or not there was a where statement present and add it if not, and you'd have to apply it only to certain tables. On the surface, it appears that it would be an attractive option but, in reality, it would cause problems. And what about those cases where you want to check for deleted records, so you end up having IsNull(IsDeleted, 1) = 1 AND IsNull(IsDeleted, 0) = 0?
 
Share this answer
 
Comments
army_man71655 2-Jul-11 9:16am    
Thanks Pete,

I guess no one else is going to respond. Your answer was swell however I was hoping this might stimulate some dialogue. Better luck next time. Thanks for your input!
Pete O'Hanlon 3-Jul-11 16:56pm    
Why not raise this in the lounge? Make it clear you're talking conceptual here and see what interest you can provoke there.
army_man71655 4-Jul-11 17:35pm    
I'll see about doing that. Thanks for the suggestion, Pete.

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