I'm currently trying to find a solution for making the following sort of query work:
I have to cast everything as clob
because the values returned in my real queries can be in the megabyte range, with +100,000 chars.
with INVOICE_DATA(INVOICEID, BILLINGCOUNTRY, TOTAL) as (
SELECT 1 as INVOICEID, 'Canada' as BILLINGCOUNTRY, 10 as TOTAL FROM DUAL UNION ALL
SELECT 2 as INVOICEID, 'Canada' as BILLINGCOUNTRY, 20 as TOTAL FROM DUAL
)
select json_object(
key 'rows' value coalesce("j", json_array(returning clob))
returning clob
)
from (
select json_arrayagg(
json_object(
key 'InvoiceId' value "INVOICEID"
returning clob
)
returning clob
) "j"
from (
select "INVOICE_DATA".*
from "INVOICE_DATA"
)
);
I've tried as many variations of cast(json_array() as clob)
, cast(json_array(returning clob) as clob)
, etc as I can think of, but nothing seems to work.
Currently, the best solution I have been able to come up with is to generate a case when …
statement and duplicate the entire structure:
select
case when "j" is null then
json_object(
key 'rows' value json_array()
returning clob
)
else
json_object(
key 'rows' value "j"
returning clob
)
end
I feel like there's got to be a better way to do this though.