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

来源:互联网 发布:与大数据相关的股票 编辑:程序博客网 时间:2024/06/06 00:35
在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数组类型





0 0
原创粉丝点击