Search code examples
oracle-databasestored-proceduresplsqlnested-table

PLSQL error: Encountered the symbol when expecting: in procedure


I am new to PL/SQL, written a procedure and could not spot the error.
Implementation Details:
I am using few cursors to traverse through a table and store some relevant data in a nested table (TYPE .. IS TABLE OF varchar(8)).
Specifically, table prerequisites stores prereq courses of a given course. I have to find direct prereqs of a given course and the prereqs of the prereqs, if any.
Code:
precoll here is,

TYPE precoll IS TABLE of varchar2(8);

declared in the package, and procedure below is in package body. Procedure:

procedure disp_prereq(p_dept_code IN varchar2, p_courseno IN number, c_prereq OUT SYS_REFCURSOR)
IS
counter INTEGER ;
preserved INTEGER ;
prelist precoll := precoll();
row prerequisites%ROWTYPE;
BEGIN
        counter := 1;
        preserved := 1;
    cursor cr is select * from prerequisites where dept_code = p_dept_code and course# = p_courseno;
        open cr;
        loop
                fetch cr into row;
                exit when row%notfound;
                prelist(counter) := (row.pre_dept_code || row.pre_course#) ;
                counter := counter + 1;
        end loop;
        close cr;
        preserved := prelist.count;
        preserved := preserved + 1;
        counter := 1;
        cursor indr is select * from prerequisites;
        open indr;
        loop
                fetch indr into row;
                exit when row%notfound;
                if (prelist(counter) = (m.dept_code||m.course#)) then
                        prelist(preserved) := m.pre_dept_code||m.pre_course# ;
                        preserved := preserved + 1;
                end if;
                counter := counter + 1;
        end loop;
        close indr;
        open c_prereq FOR select * from prelist;
END disp_prereq;

and show error gave me:


62/9     PLS-00103: Encountered the symbol "CR" when expecting one of the
         following:
         := . ( @ % ;
And my second question, prelist is a variable of precoll (TYPE precoll IS TABLE of varchar2(8);) Can I query TYPE precoll by a select query, because it is a table of varchar?


Solution

  • Question 1: The cursors should be declared in the declaration bart of the procedure. Move the:

    cursor cr is select * from prerequisites where dept_code = p_dept_code and course# = p_courseno;
    

    line to before the BEGIN. The same goes for the other cursor (indr). (There is also an alternative syntax where you declare a cursor variable in the declaration section and then use OPEN cursor_variable FOR SELECT ....)

    Question 2: Yes. In your case:

    In the declare section:

    prelist precoll;
    cur cursor;
    

    In the code section:

    open cur for 
      SELECT * FROM table(prelist);
    

    Here is another example from Ask Tom.