tags:

views:

99

answers:

2

Learning about Explicit Cursors and trying to create my frst one.:

SET SERVEROUTPUT ON
DECLARE
 v_ename EMP.FIRST_NAME%TYPE;
 v_salary EMP.SALARY%TYPE;
 CURSOR c_emp IS SELECT first_name, salary FROM emp;
BEGIN
  OPEN c_emp;
  FETCH c_emp INTO v_ename, v_salary;
  DBMS_OUTPUT.PUT_LINE('Employee Details ' || v_ename || ' ' || v_salary)

  FETCH c_emp INTO v_ename, v_salary;
  DBMS_OUTPUT.PUT_LINE('Employee Details ' || v_ename || ' ' || v_salary)

  CLOSE c_emp;
END;

but it gives me :

FETCH c_emp INTO v_ename, v_salary;
  *
ERROR at line 10:
ORA-06550: line 10, column 3:
PLS-00103: Encountered the symbol "FETCH" when expecting one of the following:
:= . ( % ;
The symbol ";" was substituted for "FETCH" to continue.
ORA-06550: line 13, column 3:
PLS-00103: Encountered the symbol "CLOSE" when expecting one of the following:
:= . ( % ;

Any ideas ?

A: 

I believe that in Oracle you cannot FETCH <cursor> inside of the CURSOR <cursor> IS statement -- perhaps you could format your code more neatly and/or give better text explanation (ideally both!-) to help us divine what in [[expletive deleted]] you're TRYING to do?! Thanks in advance...

Alex Martelli
Formatting done.
Kirtan
+4  A: 

Have you forgotten the semicolon (;) after this line?

Line #9: DBMS_OUTPUT.PUT_LINE('Employee Details ' || v_ename || ' ' || v_salary);

Try adding it and see if the error still persists.

EDIT: Yep, it seems the missing semicolon is the problem. Updated your query. Try this.

SET SERVEROUTPUT ON
DECLARE
    v_ename EMP.FIRST_NAME%TYPE;
    v_salary EMP.SALARY%TYPE;
CURSOR c_emp IS SELECT first_name, salary FROM emp;
BEGIN
        OPEN c_emp;
        FETCH c_emp INTO v_ename, v_salary;
        DBMS_OUTPUT.PUT_LINE('Employee Details ' || v_ename || ' ' || v_salary);
        FETCH c_emp INTO v_ename, v_salary;
        DBMS_OUTPUT.PUT_LINE('Employee Details ' || v_ename || ' ' || v_salary);
        CLOSE c_emp;
END;
Kirtan
And again, two lines down.
UncleO
that was it. Thanks. -1 for brainless copying of buggy code directly from book.
udit