我很好奇你们如何处理根本没有输入的sql%rowcount
a之后没有设置的问题。FORALL
下面是我如何解决它的示例(使用变量v_rowcount
和基于count
的集合FORALL
)。但我觉得有一种更聪明的方法:
create table tst (id number); -- we start with an empty table
declare
type type_numbers is table of number;
v_numbers type_numbers;
v_rowcount number;
begin
insert into tst values (1);
DBMS_OUTPUT.put_line(sql%rowcount); -- prints 1 which is correct, 1 row inserted
delete from tst;
DBMS_OUTPUT.put_line(sql%rowcount); -- prints 1 which is correct, 1 row deleted
v_numbers := type_numbers(3,4,5);
forall v in 1 .. v_numbers.count
update tst
set id = v_numbers(v)
where id = v_numbers(v);
DBMS_OUTPUT.put_line(sql%rowcount); -- prints 0 which is correct, 0 rows updated
insert into tst values (1);
DBMS_OUTPUT.put_line(sql%rowcount); -- prints 1 which is correct, 1 row inserted
delete from tst;
DBMS_OUTPUT.put_line(sql%rowcount); -- prints 1 which is correct, 1 row deleted
v_numbers := type_numbers();
forall v in 1 .. v_numbers.count
update tst
set id = v_numbers(v)
where id = v_numbers(v);
DBMS_OUTPUT.put_line(sql%rowcount); -- prints 1 which is WRONG, 0 rows updated (this is still the sql%rowcount of the DELETE above)
forall v in 1 .. v_numbers.count
update tst
set id = v_numbers(v)
where id = v_numbers(v);
v_rowcount := 0;
if v_numbers.count > 0 then
v_rowcount := sql%rowcount;
end if;
DBMS_OUTPUT.put_line(v_rowcount); -- prints 0 which is correct, 0 rows updated
end;
/