Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Looking for help on Varrays and Loops

mr.ToddSep 12 2013 — edited Sep 20 2013

Hello,

I am new to Oracle 11g and am currently enrolled in an Intro to Oracle class at my college. I have a question concerning a project that my professor has asked us to solve. I am looking for help/hints on how to write a PL/SQL anonymous block that will process records stored in the "emp" table. I have tried googling information on "varrays" and looking in the oracle documentation for PL/SQL Collections and Record, but to no prevail. I am hoping that I will be able to get some help from this community of professionals, that i am lacking from my online professor.


The program must perform the following tasks.


  1. Declare the required types and variables to store both the employee name and salary information (i.e., a counter variable may be needed also as an index).
  2. Use a loop to retrieve the first 10 "ename" and "sal" values for records in the "emp" table , store in two  variable array of 10 elements (i.e., one array will store the name; the other array will store the salary)
  3. Use another loop to display the "ename" and "sal" information in the reverse order.


This is what my coding looks like, but will not produce anything but error messages:

--Just trying to get the ename part first--

SET SERVEROUTPUT ON

Declare

  TYPE ename_array IS VARRAY(10) OF VARCHAR2(10);

  v_names ename_array := ename_array();

BEGIN

  FOR ename_array IN 1 .. 10 LOOP

  SELECT ename INTO v_names FROM emp WHERE ROWNUM < 11;

  END LOOP;

  FOR ename_array IN REVERSE 1 .. 10 LOOP

  dbms_output.put_line(v_names);

  END LOOP;

  END;

  /

Thank you again for your time and patience with a eager to learn beginner like myself.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 18 2013
Added on Sep 12 2013
8 comments
609 views