views:

109

answers:

3

We have a database where contacts are only soft-deleted, i.e. we set a bit named deleted to true. The API handles this, but sometimes you need to run queries directly against the database and it's easy to forget that bit, and export contacts that are actually deleted. It's not a big problem, because I'm used to the system, but if I'm on holiday, or if someone else is to take over I'd like to make sure these rows are not returned - unless specifically asked for. This leaves us with the following two questions:

  1. Is there a way to tell SQL Server (2005 in this case) not to return rows if for example a bit is set to true?

  2. If so, can these rows still be retrieved if specifically asked for (where bit = 'true')?

+8  A: 

In order to select either the non-deleted rows or the deleted ones, create two views:

CREATE VIEW ActiveContacts
AS 
   SELECT (list of fields)
   FROM dbo.Contacts
   WHERE deleted = 0  

CREATE VIEW DeletedContacts
AS
  SELECT (list of fields)
  FROM dbo.Contacts
  WHERE deleted = 1

Now, you can easily just pick from either the active or the deleted contacts:

SELECT (list of fields)
FROM ActiveContacts

SELECT (list of fields)
FROM DeletedContacts

That way, you can easily encapsulate that selection criteria and if you don't return that "deleted" flag in your view, outside users won't even see that flag and won't know it's there.

Marc

marc_s
I think he is talking about a way to query the database, without needing to specify the where clause. Best option as you have mentioned, is to use a view.
Yannick M.
Yes, I meant what Tannick said. Perhaps I was a bit unclear on that.
Marcus L
@Yannick M., what are you talking about? if you just _SELECT * FROM ActiveContacts_ (using the view @marc_s shows you how to create), you only get "Active" contacts and no WHERE is necessary
KM
+1  A: 

You could create a view that hides those rows, and if you need the rows directly, you can still go to the table. Try CREATE VIEW vwBlah AS SELECT table, columns, go, here FROM TableName WHERE flag = 0.

Mitch
+5  A: 

The only way that I can see to automatically do this for you would be to create views on the relevant tables that specifically exclude the deleted rows. Then use the real tables when you need to see the deleted data.

Steve Weet
I would suggest the same. Create a view if feasable. That also takes away the responsibility of the application to filter out deleted records.
Pete
Sadly, I don't think that's an option at this point - I have a feeling this should've been done as early as the design phase. But this is good to know for future projects.
Marcus L
Well if you are in development still you could consider renaming all of the tables that contain deleted rows to tablename_base or something similar. This could well work with no code changes required. It would obviously need some testing.
Steve Weet
We're unfortunately already in production, so I can't take the risk - and as always there's no time/money for massive testing.
Marcus L