# Capturing "Affected Records" in a Stored Procedure

**URL:** <https://forums.toadworld.com/t/capturing-affected-records-in-a-stored-procedure/54075>\
**Category:** Toad for IBM DB2\
**Created:** [November 10, 2021, 6:25pm UTC](https://forums.toadworld.com/t/capturing-affected-records-in-a-stored-procedure/54075 "2021-11-10T18:25:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mojoala](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@mojoala](https://forums.toadworld.com/u/mojoala)\
**Post date:** [November 10, 2021, 6:25pm UTC](https://forums.toadworld.com/t/capturing-affected-records-in-a-stored-procedure/54075/1 "2021-11-10T18:25:23Z")

</div>

Is there a way to capture the "Affected Records" in a "Stored Procedure" ?

My Stored Proc does a series of deletions. The Powers that be wants feed back on how many records are affected by each deletion.

TIA

---

<div class="post-metadata">

**Author:** ![Shimonj](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@Shimonj](https://forums.toadworld.com/u/Shimonj)\
**Post date:** [December 12, 2021, 10:28am UTC](https://forums.toadworld.com/t/capturing-affected-records-in-a-stored-procedure/54075/2 "2021-12-12T10:28:56Z")

</div>

Hi,  
I just learned a new trick that might help you.

SELECT \* FROM OLD TABLE(  
delete from anytable where id = -9999999

);

selects all affected rows.

This can be done for an update too.

I used this to prepare undo statements for mass updates.

select "update tablename set is\_active = ", is\_active , "where id = " id , ";" from old table (  
update tablename set is\_active = 0 where is\_Active = 1

) .

The results returned all rows back to what they were, without updating records that were allready is\_active = 0 before I started.  
Shimon J.

---

<div class="post-metadata">

**Author:** ![mojoala](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@mojoala](https://forums.toadworld.com/u/mojoala)\
**Post date:** [December 17, 2021, 4:02pm UTC](https://forums.toadworld.com/t/capturing-affected-records-in-a-stored-procedure/54075/3 "2021-12-17T16:02:21Z")

</div>

Finally found the answer. Took forever.

Create Procedure PC\_DATAFIX.DeleteClientFromCase( In p\_cl\_id BigInt, In p\_case\_id BigInt, In Out p\_message VarChar(4000) )

Declare v\_rows Integer ;  
Declare a,b VarChar(100);

Delete From TCL\_AGCY\_INVLVMNT Where CAS\_ID = p\_case\_id and CL\_ID = p\_cl\_id;  
GET DIAGNOSTICS v\_rows = ROW\_COUNT;  
If v\_rows \> 0 Then Set a = chr(10) || 'TCL\_AGCY\_INVLVMNT rows deleted ' || Cast(v\_rows as VarChar(3)); End If;

```
Delete From TCL_ADPT_CHLD_RLTN_RSC Where CAS_ID = p_case_id and CL_ID = p_cl_id;
 GET DIAGNOSTICS v_rows = ROW_COUNT;
If v_rows > 0 Then Set b = chr(10) || 'TCL_ADPT_CHLD_RLTN_RSC rows deleted ' || Cast(v_rows as VarChar(3)); End If;

```

Set p\_message = Coalesce(a,'') || Coalesce(b,'');
