Method Example I

SQL> CREATE OR REPLACE TYPE  book_t  AS OBJECT (
  2    title      VARCHAR(64),
  3    author     VARCHAR(32),
  4    price      NUMBER(5, 2),
  5    stock_qty  NUMBER(4),
  6    MEMBER FUNCTION  get_discount  RETURN NUMBER );

Type created.

SQL> CREATE OR REPLACE TYPE BODY  book_t  AS
  2    MEMBER FUNCTION  get_discount  RETURN NUMBER IS
  3      clearance  number := 40;
  4      nominal    number := 10;
  5    BEGIN
  6      IF  stock_qty < 100  THEN
  7        RETURN  round( ( price * nominal / 100 ), 2 );
  8      ELSE
  9        RETURN  round( ( price * clearance / 100 ), 2 );
 10      END IF;
 11    END;
 12  END;
 13  /

Type body created.

SQL> CREATE TABLE  books  OF  book_t;

Table created.

SQL> INSERT INTO  books  VALUES(
  2    book_t( 'Oracle 12c: The Complete Reference',
  3      'Kevin Loney', 44.99, 345 ) );

1 row created.

SQL> SELECT  b.price, b.price - b.get_discount( )
  2    FROM  books b  WHERE  author = 'Kevin Loney';

     PRICE    B.PRICE-B.GET_DISCOUNT( )
----------    -------------------------
     44.99                        26.99


SQL> VARIABLE  x  NUMBER;

SQL> BEGIN
  2    SELECT  b.price - b.get_discount( )  INTO  :x
  3      FROM  books b  WHERE  author = 'Kevin Loney';
  4  END;
  5  /

PL/SQL procedure successfully completed.

SQL> PRINT  :x;

         X
----------
     26.99

ROUND(n [, m]) returns n rounded to m places right of the decimal point; e.g., ROUND(15.193, 1) = 15.2.




      I’m not a big fan of stairs. They are always up to something.