RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 738790
Accepted
ks_on_v
ks_on_v
Asked:2020-11-01 18:26:28 +0000 UTC2020-11-01 18:26:28 +0000 UTC 2020-11-01 18:26:28 +0000 UTC

如果行键和集合键匹配,则重置字段

  • 772

有PL/SQL代码:

for elem in 1 .. some_array.count loop
    UPDATE my_table
    SET name = null
    WHERE id = some_array(elem).id;
end loop;

name在这里,如果此行some_array存在,则循环中的字段设置为零。id

如何更优雅地做到这一点,一两行?

同时,rollback如果出现问题(对于任何错误),是否回滚()?

sql
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    0xdb
    2020-11-02T02:50:05Z2020-11-02T02:50:05Z

    像这样的地方,更新的第二行,如果在一行,那么你将不得不滚动:

    create type item as object (id number);
    /
    create or replace type itemsType as table of item;
    /
    create table items (id number, name varchar2(32));
    /
    insert into items select level, 'item '||level from dual
    connect by level <= 10
    ;
    
    declare
        items itemsType := itemsType (item (2), item (6), item (10));
        countNulls integer;
        countRows integer;
    begin 
        update items i set name = null where exists (
            select 1 from table (items) t where t.id = i.id);
        countRows := sql%rowcount;
        select count (1) into countNulls 
        from items 
        where name is null
        ;
        dbms_output.put_line ('updated rows '||countRows||' nulls '||countNulls);
    exception when others then 
        dbms_output.put_line ('error:'||sqlerrm||chr(10)||dbms_utility.format_error_backtrace());
        raise;
    end;
    /
    
    PL/SQL procedure successfully completed.
    
    updated rows 3 nulls 3
    

    在这种情况下,rollback不需要它,因为除了单个的任何异常之外update,不会更改任何行,即 如果修改了 999 行,并且第 1000 行发生异常,则当前行所做的所有更改update都将隐式回滚。

    通常,在这种情况下,日志记录raise就足够了,调用者决定是否回滚整个事务,重试它,或者在极少数情况下以commit.

    如果您仍然需要忽略单个异常,就像在循环中可能的那样,那么您需要添加update构造log errors。例如:

    exec dbms_errlog.create_error_log (dml_table_name => 'items');
    
    alter table items add constraint checkId check (not (id=10 and name is null));
    
       -- добавить последней строкой в update, остальное в блоке без изменений  
       log errors ('update') reject limit unlimited; 
    
    --Вывод
    PL/SQL procedure successfully completed.
    
    updated rows 2 nulls 2
    
    select ora_err_mesg$, ora_err_tag$, id, name from err$_items;
    
    ОRA_ERR_MESG$                                       ORA_ERR_TAG$ ID NAME     
    --------------------------------------------------- ------------ -- ------ 
    ORA-02290: check constraint (DB.CHECKID) violated   update       10 (null)
    

    在这种情况下,此过程应根据业务逻辑以编程方式决定是否回滚。如果是,则savepoint updItems; <логика ...> rollback to updItems;, 或将异常传递给调用程序:

    if items.count != countRows then raise exceptUpdItemsFailed; end if;
    
    • 6

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5