我有一个简单的问题:
select * from countries
结果如下:
country_name
------------
Albania
Andorra
Antigua
.....
我想在一行中返回结果,如下所示:
Albania, Andorra, Antigua, ...
Of course, I can write a PL/SQL function to do the job (I already did in Oracle 10g), but is there a nicer, preferably non-Oracle-specific solution (or may be a built-in function) for this task?
我通常使用它来避免子查询中的多行,所以如果一个人拥有多个公民身份,我不希望他/她在列表中是重复的。
我的问题是基于 SQL Server 2005上的类似问题。
更新 : 我的函数是这样的:
CREATE OR REPLACE FUNCTION APPEND_FIELD (sqlstr in varchar2, sep in varchar2 ) return varchar2 is
ret varchar2(4000) := '';
TYPE cur_typ IS REF CURSOR;
rec cur_typ;
field varchar2(4000);
begin
OPEN rec FOR sqlstr;
LOOP
FETCH rec INTO field;
EXIT WHEN rec%NOTFOUND;
ret := ret || field || sep;
END LOOP;
if length(ret) = 0 then
RETURN '';
else
RETURN substr(ret,1,length(ret)-length(sep));
end if;
end;