Retrieve number of rows updated

女生的网名这么多〃 提交于 2019-12-11 17:46:24

问题


Below Dynamic SQL is updating 1319 rows.

l_sql := 'UPDATE '||l_prefix||'CRS_CUSTOMERS SET CUSTOMER_SOURCE_REF_ID = :REF_ID EXECUTE IMMEDIATE l_sql USING i.CUSTOMER_REF_ID, i.CUSTOMER_ID;

TO_CHAR(SQL%ROWCOUNT); The rowcount output is just one? How can i use any script to retrieve actual number of rows affected?


回答1:


Your code should be like this:

l_sql := 'UPDATE '||l_prefix||'CRS_CUSTOMERS SET CUSTOMER_SOURCE_REF_ID = :REF_ID';
EXECUTE IMMEDIATE l_sql USING i.CUSTOMER_REF_ID, i.CUSTOMER_ID;
dbms_output.put_line('Updated ' || SQL%ROWCOUNT || ' rows');

However, it will not work because you specified only one bind variable (:REF_ID) but you provided two values (i.CUSTOMER_REF_ID and i.CUSTOMER_ID). Bind variables and values have to match.

If SQL%ROWCOUNT returns "1" then you updated one row - check your UPDATE statement if you are not happy with that.




回答2:


Here's how:

SQL> declare
  2    l_sql varchar2(200);
  3    l_cnt number;
  4  begin
  5    l_sql := 'update emp set comm = 100 where deptno = 20';
  6    execute immediate 'begin ' || l_sql ||'; :x := sql%rowcount; end;' using out l_cnt;
  7    dbms_output.put_line('Updated ' || l_cnt || ' rows');
  8  end;
  9  /
Updated 5 rows

PL/SQL procedure successfully completed.

SQL>


来源:https://stackoverflow.com/questions/49890243/retrieve-number-of-rows-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!