PL/SQL (MOSC)

MOSC Banner

How to use a variable from an outer FOR loop to construct an INSERT statement in an inner loop

in PL/SQL (MOSC) 2 commentsAnswered ✓

Oracle Database 19c Enterprise Edition Release 19.0.0.0.0

Oracle Linux Server 7.7

I am trying to reference a value from a FOR LOOP that I've created with a SELECT statement, as a parameter in an INSERT statement within that FOR LOOP.

More specifically, I am trying to use that parameter to assemble a db_link to pull data from.

This block of code works if I hard-code a db_link in it, as such:

BEGIN
  FOR db IN
    (
    SELECT database_sid FROM STATS.DB_INFO
    WHERE ENV='P'
    )
  LOOP 
    INSERT INTO STATS.CPU_STATS
    SELECT db.database_sid, AVG_CPU_UTILIZATION
    FROM GV$RSRCMGRMETRIC_HISTORY@ORCL.world;
  END LOOP;
END; 

Howdy, Stranger!

Log In

To view full details, sign in to My Oracle Support Community.

Register

Don't have a My Oracle Support Community account? Click here to get started.

Category Leaderboard

Top contributors this month

New to My Oracle Support Community? Visit our Welcome Center

MOSC Help Center