Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a rollback inside a INSERT AFTER or UPDATE AFTER trigger rollback the entire transaction

Does a rollback inside a INSERT AFTER or an UPDATE AFTER trigger rollback the entire transaction or just the current row that is the reason for trigger, and is it same with Commit ?

I tried to check it through my current projects code which uses MSTDC for transactions, and it appears as if though the complete transaction is aborted.

If a Rollback in the trigger does rollback the entire transaction, is there a workaround for to restrict it just the current rows.

I found a link for sybase on this, but nothing on sql server

like image 627
Vamsi Avatar asked Jul 13 '12 11:07

Vamsi


People also ask

What will happen when a rollback statement is executed inside a trigger?

When the rollback trigger is executed, Adaptive Server aborts the currently executing command and halts execution of the rest of the trigger.

Does rollback work on insert?

The INSERT will automaticaly commit or, in the event of an error, rollback. If the INSERT is not in an explict transaction, SQL Server automaically performs the opertation in an implict transaction to guarantee atomic behavior.

What happens when rollback is executed?

By rolling back to an active transaction, the database can be restored to a consistent state. When the ROLLBACK command is executed, the changes made to database files, updates, additions, or deletions since the commitment boundary are rolled back and the original entries are put back in the files.

Is it possible to COMMIT rollback when a trigger has been executed?

In a procedure definition, you can use COMMIT and ROLLBACK statements. But in a trigger body, you cannot use COMMIT and ROLLBACK statements. You can use only the WHENEVER SQLERROR ABORT statement.


2 Answers

Yes it will rollback the entire transaction.

It's all in the docs (see Remarks). Note the comment I've emphasised - that's pretty important I would say!!

If a ROLLBACK TRANSACTION is issued in a trigger:

All data modifications made to that point in the current transaction are rolled back, including any made by the trigger.

The trigger continues executing any remaining statements after the ROLLBACK statement. If any of these statements modify data, the modifications are not rolled back. No nested triggers are fired by the execution of these remaining statements.

The statements in the batch after the statement that fired the trigger are not executed.

like image 174
RB. Avatar answered Sep 17 '22 23:09

RB.


As you've already been let to know, the ROLLBACK command can't possibly be modified/tuned so that it only roll back the statements issued by the trigger.

If you do need a way to "rollback" actions performed by the trigger only, you could, as a workaround, consider modifying your trigger in such a way that before performing the actions, the trigger makes sure those actions do not produce exceptional situations that would cause the entire transaction to rollback.

For instance, if your trigger inserts rows, add a check to make sure the new rows do not violate e.g. unique constraints (or foreign key constraints), something like this:

IF NOT EXISTS (
  SELECT *
  FROM TableA
  WHERE …  /* a condition to test if a row or rows you are about
              to insert aren't going to violate any constraint */
)
BEGIN
  INSERT INTO TableA …
END;

Or, if your trigger deletes rows, check if it doesn't attempt to delete rows referenced by other tables (in which case you typically need to know beforehand which tables might reference the rows):

IF NOT EXISTS (
  SELECT * FROM TableB WHERE …
)
AND NOT EXISTS (
  SELECT * FROM TableC WHERE …
)
AND …
BEGIN
  DELETE FROM TableA WHERE …
END

Similarly, you'd need to make checks for update statements, if any.

like image 27
Andriy M Avatar answered Sep 19 '22 23:09

Andriy M