首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新记录时的SQLrowrowcount

问更新记录时的SQLrowrowcount
EN

Stack Overflow用户
提问于 2020-07-11 06:15:58
回答 1查看 33关注 0票数 0

当我有多条update语句时,请协助:

代码语言:javascript
复制
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的计数,因为它只是计算完成的最后一次更新。我不确定如何合并更新的所有行的计数?

EN

回答 1

Stack Overflow用户

发布于 2020-07-11 06:41:32

rowcount仅引用最后一条语句。您可以使用变量来跟踪:

代码语言:javascript
复制
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;

或者,您可以在此处执行一次更新:

代码语言:javascript
复制
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是一个数字列,因此使用数字而不是字符串来引用它们;如果是字符串,则在示例代码中使用反号而不是引号。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62843027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档