【问题标题】:Sending PL/SQL block to db using OCI always return rows_affected=1 (ORACLE DB)使用 OCI 将 PL/SQL 块发送到 db 总是返回 rows_affected=1 (ORACLE DB)
【发布时间】:2020-07-14 11:45:41
【问题描述】:

我正在用 C 编写一个向 Oracle DB 发送查询的客户端,当我尝试对受它影响的行数进行采样时,我总是得到 1 的值。 这是查询:

char * query =    "BEGIN "\
                  "INSERT INTO table_name ( field_1, field_2, id, field_3 ) VALUES ( :1, :2, :3, 1 );"\
                  "EXCEPTION "\
                  "when dup_val_on_index then "\
                  "UPDATE table_name SET field_1 =:4, field_2 =:5, field_3 = 1 where id = :6 and field_4 <= :7;"\
                  "END; ";

我使用 OCI 的 OCIStmtExecute 函数和 OCI_COMMIT_ON_SUCCESS 执行它,并且为了对受影响的行进行采样:

ub4 update_count;
OCIAttrGet(stmthp, OCI_HTYPE_STMT, &update_count, 0, OCI_ATTR_ROW_COUNT, ctx->hp);

出于某种奇怪的原因,update_count 始终为 1。 这很奇怪,因为如果我以完全相同的方式发送简单的更新或插入查询,我会在 update_count 中得到正确的结果。

以这种形式执行查询时,有什么不同应该做的吗?

【问题讨论】:

    标签: sql c oracle oracle-call-interface


    【解决方案1】:

    不幸的是,事情就是这样运作的。您的块可能正在删除 20 行,然后更新 10 行,等等。SQL%ROWCOUNT 在通过立即执行成功执行 PL/SQL 块后总是返回 1。您可以考虑在动态块中调用 SQL%ROWCOUNT,然后返回该值并使用它。

    您可以在我的简单 LiveSQL 脚本中看到这种行为:https://livesql.oracle.com/apex/livesql/s/kdh6dang21mt8eumn4x6wv0ct

    begin 
       execute immediate 'begin null; end;'; 
       dbms_output.put_line ('count = ' || sql%rowcount); 
    end;   
    /
    
    
    count = 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-25
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 2020-03-22
      • 2012-02-23
      • 1970-01-01
      相关资源
      最近更新 更多