I have inherited a nightmarish WTF sproc that inserts 300k rows into a table of outbound marketing emails. We want to not queue messages to email addresses that are bouncing. We have a separate table of bouncing emails.
The right way to do this is to modify the sproc to not insert rows for bouncing email.
Nobody wants to touch the nightmarish clusterfuck that is this particular sproc. We are considering adding a delete command to the sproc thusly.
BEGIN TRANSACTION
--400+ lines of nightmarish WTF T-SQL, string and XML replacement nonsense goes here--
DELETE FROM EmailQueueItems
WHERE ToAddress IN
(SELECT EmailStatuses.Email FROM EmailStatuses
INNER JOIN EmailEventTypes on EmailEventTypes.EmailEventTypeId = EmailStatuses.EmailEventTypeId
WHERE EmailEventTypes.CanSendMarketing = 0)
COMMIT TRANSACTION
so…. will this work? Can I delete inserts from a table before the transaction commits?
Yes, you can insert records, and delete same, to a table within the transaction.
Try this sample for proof of concept: