Oracle 更新记录时的SqlRowCount
当我有多个更新声明时,请协助:Oracle 更新记录时的SqlRowCount,oracle,plsql,sql-update,Oracle,Plsql,Sql 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('Upda
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行,即使我的行被成功更新,我总是只得到一个计数,因为它只是计算上次完成的更新。我不确定如何合并已更新的所有行的计数?该
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
是一个数字列,所以使用数字来表示它们,而不是字符串;如果是字符串,则在示例代码中使用反勾号而不是引号。ROWCOUNT返回受上一条语句影响的行数。这实际上取决于您如何生成sql以及如何处理结果。作为一个选项,您可以使用触发器记录所有更改,然后使用此日志。