I’ve got a table, from which I constantly want all of the data (it’s a buffer being populated by another program). I also want this data to be deleted from the table once I’ve read it. So I’ve got something like
BEGIN TRANS T1;
DELETE FROM table_name OUTPUT DELETED.*;
COMMIT TRANS T1;
In the reader program I’m using a DataReader to loop through each of the rows. What I’m worried about is, what if something goes wrong while reading in this data? Say, the thread crashes and I’ve only read in part of the data. If this is a transaction, at what point is it committed? When I start reading it out? I would prefer against locking this table for a while since there’s another program always trying to write to it.
Is there perhaps a better way to do this? I have a program that’s constantly writing one record after another to this table, and I want this program to go in and get it. I’m grabbing it all at once in handfuls so that I can use DELETE – OUTPUT DELETED, instead of a more costly series of “delete where” clauses. I would prefer a way that both the writer and the reader can keep going uninterrupted.
There is no way to be sure with this method.
I would suggest something like this.
Move records to be deleted to staging table.
Output staging table contents to application
Get confirmation from application that contents have been processed.
Delete contents from staging table.