on 2021 Dec 29 5:41 AM
Assume I have the following to tables with data
create or replace table tbl_Parent (Id integer default autoincrement,Father varchar(64)); create or replace table tbl_Child (Id integer default autoincrement,ParentId integer,Name varchar(64),Gender varchar(8),Age integer); insert into tbl_Parent(Father) values ('Peter Tosh'); insert into tbl_Parent(Father) values ('Jim Carrey'); insert into tbl_Child (ParentId,Name,Gender,Age) values(1,'Jawara' ,'Male' ,40); insert into tbl_Child (ParentId,Name,Gender,Age) values(1,'Andrew' ,'Male' ,38); insert into tbl_Child (ParentId,Name,Gender,Age) values(1,'Niambe' ,'Female',37); insert into tbl_Child (ParentId,Name,Gender,Age) values(1,'Aldrina','Female',35); insert into tbl_Child (ParentId,Name,Gender,Age) values(1,'Steve' ,'Male' ,33);
I created the following domains:
create domain domain_Child row( Name tbl_Child.Name%type ,Gender tbl_Child.Gender%type ,Age tbl_Child.Age%type); create domain domain_Parent row( Name tbl_Parent.Father%type ,Children array of domain_Child);
create or replace function GetChildRows(in in_ParentId integer) returns array(32) of domain_Child begin declare array_Child array(32) of domain_child; // select array_agg(row(Name, Gender, Age) order by Age) into variable array_child from tbl_Child where ParentId = in_ParentId; // return array_Child; end;I want to create a proper json using the following statement
create or replace variable array_Parents array of domain_Parent;The following result is returned:select array_agg(row( Father,GetChildRows(Id)) order by Id) into array_Parents from tbl_Parent;
select array_Parents as Parents for json raw;
[{"Parents": [{"Father":"Peter Tosh", "Children":[{"Name":"Steve","Gender":"Male","Age":33}, {"Name":"Aldrina","Gender":"Female","Age":35}, {"Name":"Niambe","Gender":"Female","Age":37}, {"Name":"Andrew","Gender":"Male","Age":38}, {"Name":"Jawara","Gender":"Male","Age":40} ]}, {"Father":"Jim Carrey", "Children":[]} ] }]However if I want to sort the parents on the name of the father
create or replace variable array_Parents array of domain_Parent;This will result in:select array_agg(row( Father,GetChildRows(Id)) order by Father) into array_Parents from tbl_Parent;
select array_Parents as Parents for json raw;
[{"Parents": [{"Father": "Jim Carrey", "Children": [] }, {"Father": "Peter Tosh", "Children": [null,null,null,null,null] }] }]This is probably caused by the fact that the first Children object has no result and then the array of the second object for some reason is cleared.
How can I create a case to report the issue and hopefully get it solved as soon as possible?
User | Count |
---|---|
79 | |
11 | |
10 | |
10 | |
10 | |
8 | |
7 | |
7 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.