Coding⏱️ 3 min read📅 2026-06-03

How to Fix: Number of rows affected by an UPDATE in PL/SQL

Find the number of rows affected by an UPDATE in PL/SQL

Quick Answer: Use the ROWCOUNT variable to get the number of rows affected.

The issue of determining the number of rows affected by an UPDATE statement in PL/SQL can be frustrating for developers, especially when working with large datasets. This problem occurs because Oracle does not provide built-in functionality to retrieve the row count directly from the UPDATE statement.

However, there are alternative methods to achieve this, and we will outline them below.

🛑 Root Causes of the Error

  • The primary reason for this issue is that Oracle's UPDATE statement only returns a boolean value indicating whether the operation was successful or not. This means that the actual number of rows affected cannot be determined directly from the statement.
  • Another possible cause could be incorrect indexing or constraints on the table being updated, which might lead to unexpected behavior.

🚀 How to Resolve This Issue

Using the RETURNING clause

  1. Step 1: To find out how many rows were affected by an UPDATE statement in PL/SQL, you can use the RETURNING clause. This clause allows you to specify a variable that will hold the number of rows affected.
  2. Step 2: First, declare a variable before the UPDATE statement: `V_ROW_COUNT NUMBER;`
  3. Step 3: Next, modify the UPDATE statement to include the RETURNING clause: `UPDATE your_table SET column1 = value1 WHERE condition = value2 RETURNING V_ROW_COUNT INTO :V_ROW_COUNT;`
  4. Step 4: Finally, retrieve the value of the variable after the UPDATE statement has completed: `SELECT * FROM dual WHERE 1=0 AND V_ROW_COUNT = ?;`

Using a trigger

  1. Step 1: Another approach is to use a trigger that will fire before or after the UPDATE statement. This can provide additional information about the number of rows affected.
  2. Step 2: Create a trigger function: `CREATE OR REPLACE TRIGGER your_trigger_name BEFORE/AFTER UPDATE ON your_table FOR EACH ROW BEGIN V_ROW_COUNT := :NEW.V_ROW_COUNT; END;`
  3. Step 3: Modify the UPDATE statement to call the trigger function: `UPDATE your_table SET column1 = value1 WHERE condition = value2 AND V_ROW_COUNT > 0;`

🎯 Final Words

In conclusion, while Oracle does not provide built-in functionality to retrieve the row count directly from an UPDATE statement, there are alternative methods available. Using the RETURNING clause or creating a trigger function can help you determine the number of rows affected by an UPDATE operation.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions