Search code examples
loopsplsqlcursor

Procedure with Implicit cursor is compiling but not printing


i am currently stuck on this pl/sql problem, i am trying to gather all the information of a APPLICANT who APPLIES to a certain POSITION (3 different tables) into a stored procedure.

Unfortunately i am very new to oracle and pl/sql so i think my joins may be sloppy aswell as my main problem of dbms_output.put_line is not printing out the data that i need. I figure maybe it is in the wrong place in the code block or there is a problem coming all the way down from my join statements.

enter code here 
SET ECHO ON
SET FEEDBACK ON
SET LINESIZE 100
SET PAGESIZE 100
SET SERVEROUTPUT ON

CREATE OR REPLACE PROCEDURE APPLICANTS IS
first_name APPLICANT.FNAME%TYPE;
last_name APPLICANT.LNAME%TYPE;
position_number APPLIES.PNUMBER%TYPE;
position_title POSITION.TITLE%TYPE;
str VARCHAR(300);

CURSOR fnameCursor IS
SELECT FNAME
FROM APPLICANT;  

BEGIN
FOR fnameCursor IN (SELECT APPLICANT.LNAME, APPLIES.PNUMBER, 
POSITION.TITLE INTO last_name, position_number, position_title
                    FROM APPLICANT JOIN APPLIES ON APPLICANT.ANUMBER = 
APPLIES.ANUMBER
                    JOIN POSITION ON POSITION.PNUMBER = APPLIES.PNUMBER
                    WHERE FNAME = first_name
                    ORDER BY LNAME DESC)
LOOP

str := position_number || '' || first_name || '' || last_name || ': ' || 
position_title;
dbms_output.put_line(str);

--EXIT WHEN fnameCursor%NOTFOUND;
END LOOP;

END APPLICANTS;
/

EXECUTE APPLICANTS;

Solution

  • It is surprising to know that the procedure is compiling. You are using an INTO clause inside an implicit cursor query. Also, I believe first_name should come as an argument to your procedure but you have not mentioned it. More importantly, the columns selected/aliased within the cursor should be referred within the loop using cursor's record variable fnamecursor

    CREATE OR REPLACE PROCEDURE APPLICANTS(first_name APPLICANT.FNAME%TYPE)
    IS
      str VARCHAR(300);
    BEGIN
         FOR fnamecursor IN (
              SELECT applicant.lname as last_name,
                     applies.pnumber as position_number,
                     position.title  as position_title
              FROM applicant
              JOIN applies  ON applicant.anumber = applies.anumber
              JOIN position ON position.pnumber = applies.pnumber
              WHERE fname = first_name
              ORDER BY lname DESC
         ) LOOP
              str := fnamecursor.position_number || ' '   || first_name || ' ' || 
                           fnamecursor.last_name || ': ' || fnamecursor.position_title;
              dbms_output.put_line(str);
    
         END LOOP;
    END applicants;
    /