Unable to conditionally commit/rollback transaction I am trying to create a reuable SQL script that I can run that will guard against unintentional db changes based on the expected number of affected rows (not looking to create a sproc/func at this time.)

Using the answer to this SO question, I tried implementing the same but am not having much luck.

I have...

/*create table test (
    val int
);
insert into test (val) values (1);
insert into test (val) values (2);
insert into test (val) values (3);
insert into test (val) values (4); */


SET SQL_SAFE_UPDATES = 0;
set @@autocommit = 0; /* troubleshooting, doesnt seem to affect outcome */
start transaction;

set @expected = 2;  /* should cause rollback */

delete from test where val &gt; 3;

set @rc = Found_rows();
select @rc as row_count;

set @tran = (select if(@rc = @expected,'ROLLBACK','COMMIT'));
prepare stmt from @tran;
execute stmt;

select * from test;

SET SQL_SAFE_UPDATES = 1;


But it appears to be committing the transaction no matter what.

I don't imagine it matters, but this is on an AWS RDS (10.0.24-MariaDB) via MySQL Workbench 6.3

I'm more familiar with MSSQL (if that helps any.)

PS: Also found this suggestion (which I am less certain about.)
There are two errors in your script.

First, delete does not affect "Found_rows()" if you don't have a "returning".  You should use

set @rc = row_count();


Second, your logic in the if(cond,trueval,falseval) is inversed, you should use

set @tran = (select if(@rc = @expected,'COMMIT','ROLLBACK'));


You want to commit when the result equals what you expected, don't you?