Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getting `coalesce()` to work with `json_array(returning clob)`. Error: ORA-65513: value LOB operand mismatch for SQL operator

user-99gkcJun 22 2023

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.

This post has been answered by Paulzip on Jun 22 2023
Jump to Answer
Comments
Post Details
Added on Jun 22 2023
5 comments
1,276 views