【问题标题】:Oracle 11g sql%notfound issueOracle 11g sql%notfound 问题
【发布时间】:2012-12-19 11:22:14
【问题描述】:

我们已将 .database 从 oracle 8 迁移到 oracle 11g

在 update 语句之后的过程之一中,有一个 if 条件检查是否有任何行受到影响。 如果是,那么它什么也不做,否则它会将数据插入表中

IF (SQL%NOTFOUND) THEN
        -- The record does not exist so try to insert the master customer data.
        insert_order_master_customer(p_host_country_id,
                  p_order_id,             p_accting_year,
                  p_master_cust_id,
                  p_master_cust_name     );
    END IF;

但在成功更新后,此条件不起作用,它正在评估 true 并且控制进入 if 块。

【问题讨论】:

    标签: oracle oracle11g


    【解决方案1】:

    不会在 11.2.0.2 上重现。

    SQL> create table foo(id number);
    
    Table created.
    
    SQL> insert into foo values (1);
    
    1 row created.
    
    SQL> set serverout on
    SQL> begin
      2    update foo set id = 2 where id = 1;
      3     IF (SQL%NOTFOUND) THEN
      4       dbms_output.put_line('not found!');
      5    elsif (SQL%NOTFOUND = false)
      6     then
      7       dbms_output.put_line('found!');
      8     end if;
      9  end;
     10  /
    found!
    

    是在检查之前的更新,即它之间没有其他东西吗?如果您将dbms_output.put_line(sql%rowcount); 放在 IF 检查之前,还有什么输出?

    【讨论】:

    • 是的,更新语句就在检查之前。更新后我们写了提交;之后 dbms_output.put_line(sql%rowcount);给出零行。但是如果我写 dbms_output.put_line(sql%rowcount);在提交之前它给出了 1 行。
    • @aru 这就是为什么我说是检查前的更新。 COMMIT; 位于更新和 sql% 检查之间,我希望它返回 0 表示行数,返回 true 表示未找到。您必须在执行任何其他 sql 之前检查 sql,包括 commit;
    • 当我独立执行它执行正确时,更新 sql 很好。你能告诉我成功更新应该 sql% retun numbe rof rows updated of alwayz 0 吗?
    • @aru sql%rowcount 将返回更新的行数。只有在提交或其他 SQL 操作完成之前捕获它,它才会返回正确的值。
    • @aru 即。做update x set a=... where ..; 然后在之后,在任何提交之前像v_rows := sql%rowcount; 然后你有结果并且可以提交或其他什么,然后你可以检查if (v_rows > 0) then ..(同样适用于sql%notfound,只需保存到一个布尔变量)
    【解决方案2】:
    exception when DATA_NOT_FOUND then
    
    insert_order_master_customer(p_host_country_id,
                          p_order_id,             p_accting_year,
                          p_master_cust_id,
                          p_master_cust_name     );
    

    【讨论】:

    • SQL%notfound 有什么问题吗?它在 oracle 8 中运行良好
    • 是的,我们有更新声明。就在我们提交之后,我们有一个检查 IF (SQL%NOTFOUND) THEN insert command end if 。它在 oracle 8 中完全可以正常工作,即更新后它不在 if 条件内,但在迁移到 11g 后它即使在成功更新后也无法正常工作。
    【解决方案3】:
    1. 使用 SQL%ROWCOUNT 计算受影响的行数。 %NOTFOUND 仅适用于 Open-Fetch 游标。 SQL%NOTFOUND 在 WHEN NO_DATA_FOUND 异常中为 TRUE。

    2. 是,NO_DATA_FOUND,不是 DATA_NOT_FOUND

    【讨论】:

      猜你喜欢
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 2018-03-07
      • 2016-07-07
      • 1970-01-01
      相关资源
      最近更新 更多