Nested Cursors
514345Jul 31 2006 — edited Aug 1 2006I'm trying to convert my look-up table data. But before I update the actual look-up tables themselves, I want to update the tables that "look" to the look-up tables. Hard to describe, but I posted on something similar to this before. I thought I had it worked out, but it's more complicated than I realized.
I want to use a table I've created just for this called MATRIX_DEPT which houses the old and new dept. codes. Then my proc is supposed to look to the data dictionary table and update all the tables in the database that look to the DEPT table with the new dept. codes. I swear this ALMOST works!!
Here's the code so far:
CREATE OR REPLACE PROCEDURE CONVERSION IS
CURSOR look_up IS
SELECT TABLENAME, COLUMNNAME
FROM DICTIONARY
WHERE REFTABLE = 'DEPT';
CURSOR matrix IS
SELECT OLD_CODE, NEW_CODE
FROM DEPT_MATRIX
ORDER BY OLD_CODE;
DDTBL VARCHAR2(30);
DDCOL VARCHAR2(30);
OLDCODE VARCHAR2(10);
NEWCODE VARCHAR2(10);
begin
OPEN look_up;
LOOP
FETCH look_up INTO DDTBL, DDCOL;
EXIT WHEN look_up%NOTFOUND;
OPEN matrix;
LOOP
FETCH matrix INTO OLDCODE, NEWCODE;
EXIT WHEN matrix%NOTFOUND;
UPDATE DDTBL SET DDCOL = NEWCODE WHERE DDCOL = OLDCODE;
END LOOP;
CLOSE matrix;
END LOOP;
COMMIT;
CLOSE look_up;
END;
Keep in mind - this ALMOST works!! The only part that's not working is the update statement. If I comment out the update statement and instead output the new and old codes, then the script appears to work exactly as coded.
I looked into DBMS_SQL.PARSE() but couldn't get it work for this instance. All the examples I found on it were for just one cursor, not 2 nested cursors.
Would it be better just to spool the update out and then run THAT script. Kind doesn't seem as slick though.