Oracle Methods


Method is block of PL/SQL code used to encapsulate the data access method for an object, it is also specified as part of the abstract datatype specification as (CREATE OR REPLACE TYPE  MARKS_TY AS OBJECT) and their body declaration as (CREATE OR REPLACE TYPE BODY MARKS_TY AS MEMBER FUNCTION TOTMARKS()). Before to learn methods it is recommanded to get overview the abstract datatype teqniques. 

Creating specification

CREATE OR REPLACE TYPE MARKS_TY AS OBJECT (M1 NUMBER(3), M2 NUMBER(3), M3 NUMBER(3), MEMBER FUNCTION TOTMARKS(M1 IN NUMBER, M2 IN NUMBER, M3 IN NUMBER) RETURN NUMBER);

In above example, TOTMARKS is declaration of function which will be used later in function calling to get defined. 

Defining function (body declaration)

CREATE OR REPLACE TYPE BODY MARKS_TY AS MEMBER FUNCTION TOTMARKS(M1 NUMBER, M2 NUMBER, M3 NUMBER) RETURN NUMBER 
IS 
BEGIN
RETURN(M1+M2+M3);
END;
END;
/

Creating table using above abstract datatype MARKS_TY

CREATE TABLE STDMARKS
(STUDENT_ID NUMBER(4), MARKS MARKS_TY);

Inserting data into table

INSERT INTO STDMARKS VALUES
(1, MARKS_TY(85,84,73));

Selecting data from table

SELECT S.MARKS.TOTMARKS(S.MARKS.M1, S.MARKS.M2, S.MARKS.M3)
FROM STDMARKS S;

Comments

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples