Home > Blockchain >  Is it possible to define a record as the union of 2 records
Is it possible to define a record as the union of 2 records

Time:11-01

declare
     type a is record(a1 number, a2 varchar2(10));
     type b is record(b1 number, b2 varchar2(10));
     type c is record(a1 number, b2 varchar2(10),c1 number, c2 varchar2(10));
begin
    null;
end;

the record c is defined like that: the fields of c are the field of a b.

I have a real example with a lot of field. Is there a more efficient way to declare c.

Something like that ?

 type c is record( a..., c...);

And more importantly I would like thatif I change the definition of a or b, the definition of c change too. code on dbfiddle

CodePudding user response:

%ROWTYPE only applies to tables.  Just do this:

declare
     type a is record(a1 number, a2 varchar2(10));
     type b is record(b1 number, b2 varchar2(10));
     type c is record(a2 a, b2 b);
begin
    null;
end;

Yes if you change a or b, then c will change

CodePudding user response:

Is it possible to define a record as the union of 2 records?

No, Oracle does not support:

  • the ... spread operator so type c is record( a..., c...) is invalid syntax.
  • the %ROWTYPE attribute on records.

You either need to:

  • manually list all the attributes:

    type c is record(a1 number, a2 varchar2(10), b1 number, b2 varchar2(10));
    

    and then update the record whenever types a or b change.

  • use nested records:

    type c is record(x a, y b);
    v_c c;
    

    but then you have to address the types as v_c.x.a1 or v_c.y.b2 and cannot use v_c.a1 or v_c.b2 as you are not directly declaring the attributes in type c.

  • Related