当我有多条update语句时,请协助:
Update employees e
Set e.description = ‘Manager’
Where e.id = ‘1’;
Update employees e
Set e.description = ‘Supervisor’
Where e.id = ‘2’;
Update employees e
Set e.description = ‘Clerk’
Where e.id = ‘3’;
dbms_output.put_line('Updated ' || SQL%ROWCOUNT || ' rows');如何获得更新的每一行的总计数,因为实际上我正在更新140行,即使我的行被成功更新,我总是只得到1的计数,因为它只是计算完成的最后一次更新。我不确定如何合并更新的所有行的计数?
发布于 2020-07-11 06:41:32
rowcount仅引用最后一条语句。您可以使用变量来跟踪:
declare
l_counter pls_integer := 0;
begin
Update employees e
Set e.description = 'Manager'
Where e.id = 1;
l_counter := l_counter + sql%rowcount;
Update employees e
Set e.description = 'Supervisor'
Where e.id = 2;
l_counter := l_counter + sql%rowcount;
Update employees e
Set e.description = 'Clerk'
Where e.id = 3;
l_counter := l_counter + sql%rowcount;
dbms_output.put_line('Updated ' || l_counter || ' rows');
end;或者,您可以在此处执行一次更新:
begin
Update employees e
Set e.description =
case e.id
when 1 then 'Manager'
when 2 then 'Supervisor'
when 3 then 'Clerk'
end
Where e.id in (1, 2, 3);
dbms_output.put_line('Updated ' || SQL%ROWCOUNT || ' rows');
end;对于这两者,我假设id是一个数字列,因此使用数字而不是字符串来引用它们;如果是字符串,则在示例代码中使用反号而不是引号。
https://stackoverflow.com/questions/62843027
复制相似问题