Skip to content

Instantly share code, notes, and snippets.

@flash-gordon
Created January 12, 2013 11:51
Show Gist options
  • Save flash-gordon/4517490 to your computer and use it in GitHub Desktop.
Save flash-gordon/4517490 to your computer and use it in GitHub Desktop.
wm_concat/listagg alt (10gR2) no benchmarks
create or replace function concat_strings (
c_list sys_refcursor)
return varchar2
as
vch_result varchar2(4000);
vch_string varchar2(4000);
begin
loop
fetch c_list into vch_string;
exit when c_list%notfound;
if nvl(lengthb(vch_string), 0) + nvl(lengthb(vch_result), 0) > 4000 then
exit;
else
if vch_result is null then
vch_result := vch_string;
else
vch_result := vch_result || ', ' || vch_string;
end if;
end if;
end loop;
if c_list%isopen then
close c_list;
end if;
return vch_result;
exception
when others then
if c_list%isopen then
close c_list;
end if;
raise;
end concat_strings;
with letters as (
select 'a' from dual
union all
select 'b' from dual
union all
select 'd' from dual)
select concat_strings(cursor(select * from letters)) res
from dual
RES
----------------
a, b, d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment