Mega Code Archive

 
Categories / Oracle PLSQL / Object Oriented Database
 

This script demonstrates the user-defined constructor method

SQL> SQL> SQL> CREATE OR REPLACE TYPE PriceType AS OBJECT (   2     discount_rate   NUMBER (10, 4),   3     price           NUMBER (10, 2),   4     CONSTRUCTOR FUNCTION PriceType (price NUMBER)   5        RETURN SELF AS RESULT   6  )   7  INSTANTIABLE FINAL;   8  / SQL> SQL> CREATE OR REPLACE TYPE BODY PriceType   2  AS   3     CONSTRUCTOR FUNCTION PriceType (price NUMBER)   4        RETURN SELF AS RESULT   5     AS   6     BEGIN   7        SELF.price := price * .9;   8        RETURN;   9     END PriceType;  10  END;  11  / Warning: Type Body created with compilation errors. SQL> show errors Errors for TYPE BODY PRICETYPE: LINE/COL ERROR -------- ----------------------------------------------------------------- 3/25     PLS-00539: subprogram 'PRICETYPE' is declared in an object type          body and must be defined in the object type specification 3/25     PLW-07203: parameter 'SELF' may benefit from use of the NOCOPY          compiler hint 5/20     PLS-00538: subprogram or cursor 'DISCOUNT_PRICE' is declared in          an object type specification and must be defined in the object          type body SQL> SQL> SQL> SQL>  SET SERVEROUTPUT ON SIZE 1000000 SQL>  DECLARE   2      v_price   PriceType := PriceType (75);   3   BEGIN   4      DBMS_OUTPUT.put_line (v_price.price);   5   END;   6   / SQL>