问题
I am using this query
HR--Any baby with a HR<80
AS
(SELECT fm.y_inpatient_dat, h.pat_id, h.pat_enc_csn_id,
LISTAGG(meas_value, '; ') WITHIN GROUP (ORDER BY fm.recorded_time) abnormal_HR_values
from
ip_flwsht_meas fm
join pat_enc_hsp h on fm.y_inpatient_dat = h.inpatient_data_id
where fm.flo_meas_id in ('8' ) and (to_number(MEAS_VALUE) <'80')
AND fm.recorded_time between (select start_date from dd) AND (select end_date from dd)
group by fm.y_inpatient_dat,h.pat_id, h.pat_enc_csn_id)
and I get the following error:
ORA-01489: result of string concatenation is too long
I have researched online how to set a size limit, but I can't seem to make it work. Can someone please advise how to set a limit so it does not exceed the 50 characters.
回答1:
As other commentators already said, there is no way to avoid such error until Oracle 12.2 (where List_agg has the new option "ON OVERFLOW TRUNCATE").
In previous versions of oracle, if you concatenate strings longer than 4000 bytes, you get that error. you have NO way of preventing it.
If you still need to do that in previous versions, you have to write your own function for doing it and you need to modify your query accordingly:
This custom function might solve your problem
create or replace type TAB_STRINGS is table of varchar2(4000)
/
create or replace function My_list_agg(strings in TAB_STRINGS,
separator in varchar2,
max_len integer) return varchar2 deterministic is
result varchar2(32000);
tmp varchar2(32000);
begin
result := null;
if strings is not null then
for idx in strings.first .. strings. last loop
tmp := strings(idx);
if tmp is not null then
if result is null then
exit when length(tmp) > max_len;
result := tmp;
else
exit when(length(result) + length(separator) + length(tmp)) > max_len;
result := result || separator || tmp;
end if;
end if;
end loop;
end if;
return result;
end;
/
you need to use the CAST/COLLECT operator to use it.
this is an usage example:
select table_name,
My_list_agg(
-- first argument: array of strings to be concatenated
cast ( collect (column_name order by column_name) as TAB_STRINGS),
-- second (optional) argument: the separator
',',
-- third argument (optional): the maximum length of the string to be returned
1000
) as column_list
from user_tab_columns t
group by table_name
order by table_name
来源:https://stackoverflow.com/questions/53029759/why-do-i-get-the-following-error-listagg-function-result-of-string-concatenati