I am trying to write a procedure that will compile all invalid objects under a particular schema in the database using dynamic sql. Here is an example of the procedure I have come up with that does not compile:
CREATE OR REPLACE PROCEDURE TWPEDB.TEST
AS
CURSOR COMPILE_CURS IS
select 'alter ' || to_char(OBJECT_TYPE) || ' ' || to_char(OWNER) || '.' || to_char(OBJECT_NAME) || ' compile;'
from SYS.DBA_OBJECTS
where OWNER = 'TWPEDB'
AND STATUS = 'INVALID';
THE_STRING VARCHAR2(500);
cid INTEGER;
BEGIN
OPEN COMPILE_CURS;
LOOP
FETCH COMPILE_CURS
INTO THE_STRING;
EXIT WHEN COMPILE_CURS%NOTFOUND;
/* Open new cursor and return cursor ID. */
cid := DBMS_SQL.OPEN_CURSOR;
/* Parse and immediately execute the compile statement. */
DBMS_SQL.PARSE(cid, THE_STRING, dbms_sql.v7);
/* Close cursor. */
DBMS_SQL.CLOSE_CURSOR(cid);
END LOOP;
CLOSE COMPILE_CURS;
EXCEPTION
/* If an exception is raised, close cursors before exiting. */
WHEN OTHERS THEN
DBMS_SQL.CLOSE_CURSOR(cid);
CLOSE COMPILE_CURS;
END;
The problem I have is that this procedure does not compile because of the following error:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
Cause: In a declaration, the name of a variable or cursor is misspelled or the declaration makes a forward reference. Forward references are not allowed in PL/SQL. A variable or cursor must be declared before it is referenced it in other statements, including other declarative statements. For example, the following declaration of dept_rec raises this exception because it refers to a cursor not yet declared:
DECLARE
dept_rec dept_cur%ROWTYPE;
CURSOR dept_cur IS SELECT ...
...
I am assuming my problem is that the cursor query is referencing the DBA_OBJECTS table, of which this procedure has a record.
Do have any other ideas on how to do this? I would love to get this done in a procedure.
Thanks,
PaullyB
CREATE OR REPLACE PROCEDURE TWPEDB.TEST
AS
CURSOR COMPILE_CURS IS
select 'alter ' || to_char(OBJECT_TYPE) || ' ' || to_char(OWNER) || '.' || to_char(OBJECT_NAME) || ' compile;'
from SYS.DBA_OBJECTS
where OWNER = 'TWPEDB'
AND STATUS = 'INVALID';
THE_STRING VARCHAR2(500);
cid INTEGER;
BEGIN
OPEN COMPILE_CURS;
LOOP
FETCH COMPILE_CURS
INTO THE_STRING;
EXIT WHEN COMPILE_CURS%NOTFOUND;
/* Open new cursor and return cursor ID. */
cid := DBMS_SQL.OPEN_CURSOR;
/* Parse and immediately execute the compile statement. */
DBMS_SQL.PARSE(cid, THE_STRING, dbms_sql.v7);
/* Close cursor. */
DBMS_SQL.CLOSE_CURSOR(cid);
END LOOP;
CLOSE COMPILE_CURS;
EXCEPTION
/* If an exception is raised, close cursors before exiting. */
WHEN OTHERS THEN
DBMS_SQL.CLOSE_CURSOR(cid);
CLOSE COMPILE_CURS;
END;
The problem I have is that this procedure does not compile because of the following error:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
Cause: In a declaration, the name of a variable or cursor is misspelled or the declaration makes a forward reference. Forward references are not allowed in PL/SQL. A variable or cursor must be declared before it is referenced it in other statements, including other declarative statements. For example, the following declaration of dept_rec raises this exception because it refers to a cursor not yet declared:
DECLARE
dept_rec dept_cur%ROWTYPE;
CURSOR dept_cur IS SELECT ...
...
I am assuming my problem is that the cursor query is referencing the DBA_OBJECTS table, of which this procedure has a record.
Do have any other ideas on how to do this? I would love to get this done in a procedure.
Thanks,
PaullyB