Oracle 实现多行转换成一行 使用oracle 10g collect函数

在Oracle 10g中,新增加了一个聚合函数

collect:Takes a column of any type and creates a nested table of the input type out of the rows selected


1、创建数组类型

create or replace type varchar2_app as table of varchar2(2000);

2、创建format_string格式化输出函数

create or replace function format_string(v_table in varchar2_app) return varchar2 is
  Results varchar2(30000);
begin
  for i in 1 .. v_table.count loop
    Results :=Results||','||v_table(i);
  end loop;
 --去掉第一个逗号--
 return(substr(Results,2));
end format_string;
3、开始使用

select object_type, 
       format_string(CAST(COLLECT(object_name) AS varchar2_app)) AS object_name 
from user_objects 
group by object_type;

注意:

COLLECT函数后要用类型varchar2_app数组类型





你可能感兴趣的:(oracle)