Skip to Main Content
  • Questions
  • Return count of rows deleted by delete statement

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question.

Asked: February 04, 2005 - 11:54 am UTC

Last updated: February 05, 2005 - 5:47 am UTC

Version: 9.2.0.5

Viewed 1000+ times

You Asked

Tom,
Is there a good way to return the just the count of rows deleted by a delete statement. Currently, the developers first select the count and then delete. This is like running the same statement twice.

Thanks




and Tom said...

not only that, but the answer they give you is WRONG!!!!!!!!!!!!!!

so what if when they counted (which is really slow), there were 500 rows. By the time the count finished, maybe there were 490, or 510 or 0 or whatever.

They should have just: select dbms_random.value into :x from dual;

it would be approximately as accurate and meaningful!!!!



begin
delete from t where a = :a and b = :b and c = :c ....whatever... ;
:x := sql%rowcount;
end;


tell them to use that block of code - that'll work in any language, all languages give you a way to find the number of rows affected by your last DML as far as I know, but you didn't tell me what language you use. This approach works in all of them though.

Or you could ask them to read the documentation and learn the API they are using, to avoid wasteful, incorrect approaches to a rather simple problem!






Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, February 05, 2005 - 5:47 am UTC

We are using delete statement within procedure in a package so this will work just fine. Thanks so much.

More to Explore

DBMS_RANDOM

More on PL/SQL routine DBMS_RANDOM here