I am getting the below error after executing the code given below :
"Error1: Inside Delete Block: ORA-00904: "AJAPPDAMEAAD6HPAAF": invalid identifier"
Its not identifying the rowid value in the delete statement, the block is getting inside the refcursor and fetching the rowid for the table and then its not able to execute the delete statement for the same rowid, Please help me how to achieve this using rowid. Thanks In Advance...
CREATE OR REPLACE PROCEDURE PROCESS_TEST (V_SCHEMA VARCHAR2, V_TAB VARCHAR2) AS
l_sql VARCHAR2 (2000);
CURSOR c_x
IS
SELECT *
FROM TEST_TAB
WHERE UPPER (SCHEMA) = UPPER (V_SCHEMA)
AND UPPER (TABLE_NAME) = UPPER (V_TAB)
AND STATUS IS NULL;
TYPE tab_names_type IS TABLE OF TEST_TAB%ROWTYPE;
tab_names tab_names_type;
l_cursor SYS_REFCURSOR;
TYPE c_1 IS TABLE OF UROWID;
tab_row c_1;
l_sql_errm VARCHAR2 (2000);
V_STATUS VARCHAR2 (1) := 'N';
BEGIN
OPEN c_x;
LOOP
FETCH c_x BULK COLLECT INTO tab_names LIMIT 1000;
EXIT WHEN tab_names.COUNT = 0;
FOR id IN tab_names.FIRST .. tab_names.LAST
LOOP
l_sql :=
'SELECT a.ROWID FROM '
|| tab_names (id).SCHEMA
|| '.'
|| tab_names (id).TABLE_NAME
|| ' a';
OPEN l_cursor FOR l_sql;
LOOP
FETCH l_cursor BULK COLLECT INTO tab_row LIMIT 1000;
EXIT WHEN tab_row.COUNT = 0;
FOR i IN tab_row.FIRST .. tab_row.LAST
LOOP
BEGIN
EXECUTE IMMEDIATE
'DELETE '
|| tab_names (id).SCHEMA
|| '.'
|| tab_names (id).TABLE_NAME
|| ' a WHERE a.ROWID = '
|| tab_row (i);
COMMIT;
V_STATUS := 'Y';
EXCEPTION
WHEN OTHERS
THEN
l_sql_errm := 'Inside Delete Block: ' || SQLERRM;
V_STATUS := 'N';
ROLLBACK;
END;
END LOOP;
END LOOP;
END LOOP;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
l_sql_errm := 'Inside Others Block: ' || SQLERRM;
V_STATUS := 'N';
ROLLBACK;
END;
You are concatenating your rowid value into the delete statement as a string. If you're going to do that you need to at least quote it, and more correctly convert it via
chartorowid()(though you're still implicitly converting yoururowidvalue to a string):It would be better to use a bind variable:
or just:
db<>fiddles of your version and my version, with debugs added so you can actually see the error (it would be better to throw or log it, depending on whether you want to carry on; the handling and transaction control suggest you want to carry on, but you still need to report/record the problem reliably - which means not using
dbms_output!).