Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the number of affected rows in a MySQL update statement?

I have stored procedure in MySQL, something like the below:

create procedure SP_Test (input1 varchar(20))
begin
update Table1 set Val1='Val' where country=input1;
//I want to see if this update changed how many rows and 
//do some specific action based on this number
....
end

How can I determine how many rows were changed by this update?

like image 269
Alaa Avatar asked Nov 29 '10 17:11

Alaa


People also ask

How do you get the number of rows affected by a query?

MySQL ROW_COUNT() can be used to get the total number of rows affected by MySQL query. To illustrate it we are creating a procedure with the help of which we can insert records in a table and it will show us how many rows have been affected.

How do I get the number of rows affected in a JDBC insert statement?

Statement. executeUpdate() or execute() followed by getUpdateCount() will return the number of rows matched, not updated, according to the JDBC spec. If you want the updated count, you can specify useAffectedRows=true as a non-standard URL option.

Which function will return the number of rows affected by a query regardless of the type of query?

The affected_rows / mysqli_affected_rows() function returns the number of affected rows in the previous SELECT, INSERT, UPDATE, REPLACE, or DELETE query.

What function can determine the number of rows inserted updated or deleted?

ROW_COUNT() returns the number of rows updated, inserted or deleted by the preceding statement. This is the same as the row count that the mysql client displays and the value from the mysql_affected_rows() C API function.


1 Answers

Use ROW_COUNT():

SELECT ROW_COUNT();
like image 106
Joe Stefanelli Avatar answered Oct 31 '22 12:10

Joe Stefanelli