Create a MySQL stored procedure that counts the number of rows gets affected by MySQL query?


Following is a procedure that counts the number of rows get affected by MySQL query −

mysql> Delimiter //
mysql> CREATE PROCEDURE `query`.`row_cnt` (IN command VarChar(60000))
    -> BEGIN
    -> SET @query = command;
    -> PREPARE stmt FROM @query;
    -> EXECUTE stmt;
    -> SELECT ROW_COUNT() AS 'Affected rows';
    -> END //
Query OK, 0 rows affected (0.00 sec)

mysql> Delimiter ;

mysql> Create table Testing123(First Varchar(20), Second Varchar(20));
Query OK, 0 rows affected (0.48 sec)

mysql> CALL row_cnt("INSERT INTO testing123(First,Second) Values('Testing First','Testing Second');");
+---------------+
| Affected rows |
+---------------+
| 1             |
+---------------+
1 row in set (0.10 sec)
Query OK, 0 rows affected (0.11 sec)

Updated on: 22-Jun-2020

957 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements