Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use row_count() with transAction?

i have an stored procedure in mysql like below:

BEGIN
START TRANSACTION;  
    INSERT INTO tbl1 (v1,v2) VALUES (p1,p2);
    UPDATE tbl2 SET s1 = 1 WHERE s2 = s3;
    SELECT ROW_COUNT();
COMMIT
END

in this query even the transaction doesn't commit, the row_count will be 1. in fact my question is that how can i use row_count that it returns 0 if rollback occurred?

like image 503
Fatemeh Gharri Avatar asked Nov 19 '25 17:11

Fatemeh Gharri


1 Answers

I ran into a similar problem recently, but I managed to solve it by selecting the row count inside of the transaction block and then returning it after the commit:

BEGIN
START TRANSACTION;  
    INSERT INTO tbl1 (v1,v2) VALUES (p1,p2);
    UPDATE tbl2 SET s1 = 1 WHERE s2 = s3;
    SET @rowCount = (SELECT ROW_COUNT());
COMMIT
SELECT @rowCount;
END

I don't know if this is the best way to solve it, but it seems to work for me :-)

like image 83
Martin Avatar answered Nov 22 '25 08:11

Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!