Created
January 12, 2013 11:51
-
-
Save flash-gordon/4517490 to your computer and use it in GitHub Desktop.
wm_concat/listagg alt (10gR2) no benchmarks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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