Search code examples
sqloracleplsqluser-defined-types

Object type specification not defined in body Oracle SQL


I've got this problem with a PL/SQL object I'm trying to develop. The error message is this:

 PLS-00538: subprogram or cursor 'BASE_T' is declared in an
         object type specification and must be defined in the object type
         body

And my object is this:

-- Create the first object
CREATE OR REPLACE TYPE base_t IS OBJECT (
    oname VARCHAR2 (30), 
    name VARCHAR2 (30),
    CONSTRUCTOR FUNCTION base_t RETURN SELF AS RESULT,
    CONSTRUCTOR FUNCTION base_t (oname VARCHAR2, name VARCHAR2) RETURN SELF AS RESULT,
    MEMBER FUNCTION get_oname RETURN VARCHAR2,
    MEMBER FUNCTION get_name RETURN VARCHAR2,
    MEMBER PROCEDURE set_oname (oname VARCHAR2),
    MEMBER PROCEDURE to_string
    ) INSTANTIABLE NOT FINAL;
/

-- Body of the object
CREATE OR REPLACE TYPE BODY base_t AS 
    CONSTRUCTOR FUNCTION base_t (oname VARCHAR2 , name VARCHAR2 ) RETURN SELF AS RESULT IS BEGIN
        SELF.oname := oname;
        SELF.name := name;
    END;
    MEMBER PROCEDURE set_oname (oname VARCHAR2 ) IS BEGIN
        SELF.oname := oname;
    END set_oname;
    MEMBER FUNCTION get_oname RETURN VARCHAR2 IS BEGIN
        RETURN SELF.oname;
    END get_oname;
    MEMBER FUNCTION get_name RETURN VARCHAR2 IS BEGIN
        RETURN SELF.name;
    END get_name;
    MEMBER PROCEDURE to_string IS BEGIN
        dbms_output.put_line('Hello ['||self.oname||'].');
    END to_string; 
END;
/

The problem is, usually this error message is called by inconsistent function/procedure definitions/names (as shown in this SO question here: Oracle Error PLS-00323: subprogram or cursor is declared in a package specification and must be defined in the package body), but I've checked through and I don't believe any of my names are messed up and all of my definitions are consistent. How else can you get that error message?


Solution

  • Add some code for the default ctor into the TYPE BODY eg

    SQL> CREATE OR REPLACE TYPE BODY base_t AS 
      2  
      3      constructor function base_t return self as result 
      4      is
      5      begin
      6        self.oname := null ;
      7        self.name := null ;
      8      end ;
      9  
     10      CONSTRUCTOR FUNCTION base_t (oname VARCHAR2 , name VARCHAR2 ) RETURN SELF AS RESULT IS BEGIN
     11          SELF.oname := oname;
     12          SELF.name := name;
     13      END;
     14      MEMBER PROCEDURE set_oname (oname VARCHAR2 ) IS BEGIN
     15          SELF.oname := oname;
     16      END set_oname;
     17      MEMBER FUNCTION get_oname RETURN VARCHAR2 IS BEGIN
     18          RETURN SELF.oname;
     19      END get_oname;
     20      MEMBER FUNCTION get_name RETURN VARCHAR2 IS BEGIN
     21          RETURN SELF.name;
     22      END get_name;
     23      MEMBER PROCEDURE to_string IS BEGIN
     24          dbms_output.put_line('Hello ['||self.oname||'].');
     25      END to_string; 
     26  END;
     27  /
    
    Type Body BASE_T compiled