问题
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