Search code examples
objectplsqlinitializing

Initializing an object from subtype


I need to create and initialize an object from a subtype B, with supertype A in PL/SQL:

create or replace 
TYPE "A" as object{
school_category  varchar(10);
}

create or replace 
TYPE "B" UNDER A {
school_name varchar(10);
school_ranking INTEGER;
}

Now, I when I run the below code:

Declare 
   i_B B;
BEGIN
  i_B := B('name_sample', 12, A('elementary'));
END;

I get below error:

PLS-00306: wrong number or types of arguments in call to 'B'

I really appreciate your help on this. Thanks a lot.


Solution

  • TRY THIS CODE:-

     Create Or Replace Type Atest As Object
        (
        School_Category  Varchar2(10)
        );
    
        create or replace Type Btest Under Atest (
        school_name varchar2(10),
        School_Ranking number
             );
    
    DECLARE
      var2 Btest;
    BEGIN
      var2 := Btest('Good','MySchool',2);
    
        Dbms_Output.Put_Line(var2. School_Category);
        Dbms_Output.Put_Line(var2. school_name);
        Dbms_Output.Put_Line(var2. School_Ranking);
    End;
    

    Or

    Create Or Replace Type Atest As Object
    (
    School_Category  Varchar2(10)
    );
    
    create or replace Type Btest as Object  (
    school_name varchar2(10),
    School_Ranking number,
    School_Categ  Atest
    );
    
    Declare
    iobj Btest;
    Begin
    iobj:=Btest(school_name=>'MYSCHOOL',School_Ranking=>2,School_Categ=>Atest(School_Category=>'GOOD'));
    
    Dbms_Output.Put_Line(Iobj.School_Name);
    Dbms_Output.Put_Line(Iobj.School_Ranking);
    
    end;