Link to home
Start Free TrialLog in
Avatar of Kenya Westmoreland
Kenya Westmoreland

asked on

PL/SQL Loop through to connect to Oracle PDBS and run script

Stats:
Oracle 12c Mutlitenant
5 PDBS
RHEL v5

I have created a monitoring script that I want to run again all my pdbs in my database. I have created a simple script, just to show  the con_name but I get the error of PLS-00642 Local collection types not allowed in SQL statements at "select name into vname from v$pdbs" .Basically, I need a way to loop through the pdbs, connect to each one and output the results of the script.

DECLARE
 TYPE names_t IS TABLE OF v$pdbs.name%TYPE;
 names        names_t;
 vname        names_t;
 TYPE open_modes_t IS TABLE OF v$pdbs.open_mode%TYPE;
 open_modes   open_modes_t;
 BEGIN
 SELECT   name, open_mode
 BULK   COLLECT
 INTO   names, open_modes
 FROM   v$pdbs
 WHERE   name NOT IN ('PDB$SEED', 'SAMPLE_SCHEMAS');
 FOR j IN 1 .. names.COUNT ()
 LOOP
 IF open_modes (j) <> 'MOUNTED'
 THEN
 EXECUTE IMMEDIATE   'alter session set container= "'
 || names (j)
 || '"';
 END IF;
   select name into vname from v$pdbs;
   dbms_output.put_line ('Current pdb '||vname);
 END LOOP;
 END;
 /
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial