Show Number of Rows Affected by an INSERT, UPDATE or DELETE

When you run a DML statement like an INSERT, UPDATE or DELETE, some number of rows are affected.

Databases have various ways to let you display that number.

In MS SQL Server and Sybase, @@ROWCOUNT returns the number of rows affected by the last statement.

Oracle uses SQL%ROWCOUNT and MySQL uses the function mysql_affected_rows().

To get the same result in Postgres, and therfore YugabyteDB, we use the GET DIAGNOSTICS command.

Example:

				
					yugabyte=# DO $$
yugabyte$# DECLARE
yugabyte$#      total_rows INT;
yugabyte$# BEGIN
yugabyte$#   CREATE TABLE t (c1 INT, c2 TEXT);
yugabyte$#   INSERT INTO t VALUES (1, 'A'), (2, 'B'), (3, 'C');
yugabyte$#   GET DIAGNOSTICS total_rows := ROW_COUNT;
yugabyte$#   RAISE NOTICE 'Rows inserted : total_rows: %', total_rows;
yugabyte$#   UPDATE t SET c2 = 'Z' WHERE c1 IN (1, 3);
yugabyte$#   GET DIAGNOSTICS total_rows := ROW_COUNT;
yugabyte$#   RAISE NOTICE 'Rows updated : total_rows: %', total_rows;
yugabyte$# END $$;
NOTICE:  Rows inserted : total_rows: 3
NOTICE:  Rows updated : total_rows: 2
DO
				
			

Have Fun!