SQL%ROWCOUNT
juga dapat digunakan tanpa ditugaskan (setidaknya dari Oracle 11g ).
Selama tidak ada operasi (pembaruan, penghapusan atau penyisipan) telah dilakukan dalam blok saat ini, SQL%ROWCOUNT
diatur ke nol. Kemudian tetap dengan jumlah garis yang dipengaruhi oleh operasi DML terakhir:
katakanlah kita memiliki tabel KLIEN
create table client (
val_cli integer
,status varchar2(10)
)
/
Kami akan mengujinya dengan cara ini:
begin
dbms_output.put_line('Value when entering the block:'||sql%rowcount);
insert into client
select 1, 'void' from dual
union all select 4, 'void' from dual
union all select 1, 'void' from dual
union all select 6, 'void' from dual
union all select 10, 'void' from dual;
dbms_output.put_line('Number of lines affected by previous DML operation:'||sql%rowcount);
for val in 1..10
loop
update client set status = 'updated' where val_cli = val;
if sql%rowcount = 0 then
dbms_output.put_line('no client with '||val||' val_cli.');
elsif sql%rowcount = 1 then
dbms_output.put_line(sql%rowcount||' client updated for '||val);
else -- >1
dbms_output.put_line(sql%rowcount||' clients updated for '||val);
end if;
end loop;
end;
Yang menghasilkan:
Value when entering the block:
Number of lines affected by previous DML operation:5
2 clients updated for 1
no client with 2 val_cli.
no client with 3 val_cli.
1 client updated for 4
no client with 5 val_cli.
1 client updated for 6
no client with 7 val_cli.
no client with 8 val_cli.
no client with 9 val_cli.
1 client updated for 10