Karan
Karan

Reputation: 359

Cursor in pl/sql

I have ORACLE class airport_t that has data fields for

Rank,
Airport,
Location,
Country,
Code_IATA,
Code_ICAO,
Total_passenger,
Rank_change,
Percent_Change

I created table AIRPORTS2017OO that has one single column AIRPORT, which can store an object of type airport_t. I have 50 rows of data in Airport2017 table which I to insert into airport column of table AIRPORTS2017OO using cursor.

I am able to iterate through airports2017 table through cursor given below, but I do not know how to insert data into AIRPORTS2017OO table.

Here is my solution of getting all info from airports2017 table

DECLARE
   rank1        NUMBER;
   airports1    VARCHAR2 (80);
   location1    VARCHAR (40);
   country1     VARCHAR (30);
   iata1        VARCHAR (3);
   icao1        VARCHAR (4);
   total_pass   NUMBER;
   rank_c       NUMBER;
   p_change     NUMBER;

   CURSOR display
   IS
      SELECT * FROM airports2017;
BEGIN
   OPEN display;

   LOOP
      FETCH display
         INTO rank1,
              airports1,
              location1,
              country1,
              iata1,
              icao1,
              total_pass,
              rank_c,
              p_change;

      IF display%FOUND
      THEN
         DBMS_OUTPUT.put_line (airports1);
      ELSE
         EXIT;
      END IF;
   END LOOP;

   CLOSE display;
END;
/

Upvotes: 0

Views: 80

Answers (1)

sandman
sandman

Reputation: 2108

You do know that you can do:

INSERT INTO AIRPORTS2017OO select * from airports2017;

instead of all this cursor code right?

Anyway here's how to do it:

DECLARE    
   a_data ARRAY;
   CURSOR display
   IS
      SELECT * FROM airports2017;
BEGIN
   OPEN display;

   LOOP
      FETCH display BULK COLLECT INTO a_data;

      FORALL i IN 1..a_data.COUNT
         INSERT INTO AIRPORTS2017OO VALUES a_data(i);

      IF display%FOUND
      THEN
         DBMS_OUTPUT.put_line (airports1);
      ELSE
         EXIT;
      END IF;
   END LOOP;

   CLOSE display;
END;
/

Upvotes: 1

Related Questions