Managing transactions is generally easier to do in the host language, IMO. There, you would define a transaction on the connection to the database, begin the transaction, execute the stored procedure that contains your delete statements (or execute them individually), then on success commit the transaction or on an exception rollback the transaction, undoing all effects of the SQL statements you've executed.
This is the SQL Language forum; while the basic transaction mechanism is part of the SQL Standard, the implementation details (especially error handling and flow control) may differ between particular products.
You define and begin a transaction with the BEGIN TRANSACTION statement, commit the transaction with the COMMIT TRANSACTION statement, and rollback the transaction with ROLLBACK TRANSACTION.
The awkward part comes in detecting whether to do a COMMIT or ROLLBACK. In SQL Server's T/SQL language, the @@ERROR variable can be tested after each statement. If the value is nonzero, an error has occurred so you ROLLBACK the transaction. If all statements succeed, you COMMIT instead.
There is a whole other issue regarding the locking of data while the delete is being executed. Not only does a transaction allow you to declare a set of UPDATE/DELETE statements that should execute atomically, you can also control how others may, or may not, view the data while you are in the process of changing it, or whether or not you "see" any changes that they may be making.
That is called the transaction isolation level and that is a subject for another post.
Code to manage transactions (in T/SQL) can be structured like this:
Code:
DECLARE @GotError integer
SET @GotError = 0
BEGIN TRANSACTION
DELETE FROM <yourtable> ...
IF @@ERROR > 0 SET @GotError = @@ERROR
IF @GotError = 0 BEGIN
DELETE FROM <yourtable> ...
IF @@ERROR > 0 SET @GotError = @@ERROR
END
IF @GotError = 0 BEGIN
DELETE FROM <yourtable> ...
IF @@ERROR > 0 SET @GotError = @@ERROR
END
...
If @GotError = 0
COMMIT TRANSACTION
else
ROLLBACK TRANSACTION
Like I said, simpler in the host...
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com