Hi All, am facing a problem with SQL%ROWCOUNT returning 0, as well as with SELECT in a LOOP
SET serveroutput on;
DECLARE
TYPE date_array IS TABLE OF VARCHAR2 (12);
v_date date_array;
v_cnt_before_upd PLS_INTEGER;
v_cnt_after_upd PLS_INTEGER;
PROCEDURE update_table_desc (ip_char IN CHAR)
IS
v_desc_from VARCHAR2 (30);
v_desc_to VARCHAR2 (30);
BEGIN
IF ip_char = 'C'
THEN
v_desc_from := 'From Desc 1';
v_desc_to := 'To Desc 1';
ELSIF ip_char = 'S'
THEN
v_desc_from := 'From Desc 2';
v_desc_to := 'To Desc 2';
END IF;
SELECT COUNT (*)
INTO v_cnt_before_upd
FROM table1
WHERE col1_desc = v_desc_from;
UPDATE table1
SET col1_desc = v_desc_to
WHERE col1_desc = v_desc_from;
DBMS_OUTPUT.put_line ('sql%rowcount:..... ' || SQL%ROWCOUNT);
SELECT COUNT (*)
INTO v_cnt_after_upd
FROM table1
WHERE col1_desc = v_desc_to;
DBMS_OUTPUT.put_line ( 'To be updated: '
|| v_cnt_before_upd
|| '......Updated: '
|| v_cnt_after_upd
);
IF v_cnt_after_upd = v_cnt_before_upd
THEN
DBMS_OUTPUT.put_line ( v_desc_from
|| ' updated successfully to '
|| v_desc_to
);
COMMIT;
ELSE
DBMS_OUTPUT.put_line (v_desc_from || ' update failed');
ROLLBACK;
END IF;
EXCEPTION
WHEN OTHERS
THEN
RAISE;
END;
BEGIN
SELECT reporting_date
BULK COLLECT INTO v_date
FROM table2
WHERE TRUNC (reporting_date) >= '31-Mar-2011'
AND TRUNC (reporting_date) <= TRUNC (SYSDATE);
FOR i IN v_date.FIRST .. v_date.LAST
LOOP
pack_context.context_open (v_date (i));
DBMS_OUTPUT.put_line ('..............');
DBMS_OUTPUT.put_line ('Context: ' || v_date (i));
update_table_desc ('C');
update_table_desc ('S');
pack_context.context_disable;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (DBMS_UTILITY.format_error_stack ());
END;
/
OUTPUT:
***************************
.............. (LOOP 1)
Context: 26-OCT-12
sql%rowcount:..... 0
To be updated: 0......Updated: 573
From Desc 1 update failed
sql%rowcount:..... 0
To be updated: 0......Updated: 127
From Desc 2 update failed
.............. (LOOP 2)
Context: 02-NOV-12
sql%rowcount:..... 0
To be updated: 0......Updated: 573
From Desc 1 update failed
sql%rowcount:..... 0
To be updated: 0......Updated: 127
From Desc 2 update failed
.............. (LOOP 3)
Context: 31-AUG-12
sql%rowcount:..... 0
To be updated: 571......Updated: 573
From Desc 1 update failed
sql%rowcount:..... 0
To be updated: 127......Updated: 127
From Desc 2 updated successfully to To Desc 2
PL/SQL procedure successfully completed.
As you can see, SQL%ROWCOUNT returns 0 even when there is an update in (LOOP 3).
Also note the counts returned before (To be updated:) and after update (Updated:).
They're different in LOOP 3. But when I run the update stmt alone, am getting only the
571 records updated which is what available there. Don't understand from where the select
stmt get 573 from. Please help me understand the possibilities of getting wrong counts.
FYI: I didnt faced this issue in Oracle 11.2.0.3, am facing it in 11.2.0.1. Also the statement
pack_context.context_open will set the policy to access only the partition belonging to the
input date.