Search code examples
sqldatabaseoracleplsqlcursor

what is wrong with the syntax of cursor in oracle for my database?


SQL> CREATE TABLE DOCTOR (
  2  DID INT PRIMARY KEY NOT NULL,
  3  DNAME VARCHAR (15) NOT NULL,
  4  QUALIFICATION VARCHAR(15)
  5  );

Table created.

SQL> CURSOR C_DOCTOR IS SELECT DID,DNAME,QUALIFICATION FROM DOCTOR;
SP2-0734: unknown command beginning "CURSOR C_D..." - rest of line ignored.

I have been following the syntax for explicit cursors, however I am still getting this error. Can you please help me?


Solution

  • Cursor doesn't want to be alone in space, it requires some PL/SQL environment. That would be a PL/SQL block, its DECLARE section. The simplest way would be

    SQL> CREATE TABLE DOCTOR (
      2      DID INT PRIMARY KEY NOT NULL,
      3      DNAME VARCHAR (15) NOT NULL,
      4      QUALIFICATION VARCHAR(15)
      5      );
    
    Table created.
    
    SQL> declare
      2    CURSOR C_DOCTOR IS SELECT DID,DNAME,QUALIFICATION FROM DOCTOR;   --> this is your line
      3  begin
      4    null;
      5  end;
      6  /
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    True, quite useless, but - it compiles.