---create custom object CREATE or replace TYPE unit_type AS OBJECT ( unit1 VARCHAR2(50) ,unit2 VARCHAR2(50) ,unit3 VARCHAR2(50) ,unit4 VARCHAR2(50) ); ---create table object by type Object CREATE TYPE unit AS TABLE OF unit_type; ---create table create table t_unit (unitname varchar2(50)); ---insert into data begin insert into t_unit (UNITNAME) values ('a'); insert into t_unit (UNITNAME) values ('b'); insert into t_unit (UNITNAME) values ('c'); insert into t_unit (UNITNAME) values ('d'); insert into t_unit (UNITNAME) values ('e'); insert into t_unit (UNITNAME) values ('f'); insert into t_unit (UNITNAME) values ('g'); insert into t_unit (UNITNAME) values ('h'); insert into t_unit (UNITNAME) values ('i'); insert into t_unit (UNITNAME) values ('j'); insert into t_unit (UNITNAME) values ('k'); insert into t_unit (UNITNAME) values ('l'); insert into t_unit (UNITNAME) values ('m'); insert into t_unit (UNITNAME) values ('n'); insert into t_unit (UNITNAME) values ('o'); insert into t_unit (UNITNAME) values ('p'); insert into t_unit (UNITNAME) values ('r'); insert into t_unit (UNITNAME) values ('s'); insert into t_unit (UNITNAME) values ('t'); insert into t_unit (UNITNAME) values ('u'); insert into t_unit (UNITNAME) values ('v'); insert into t_unit (UNITNAME) values ('w'); insert into t_unit (UNITNAME) values ('x'); insert into t_unit (UNITNAME) values ('y'); insert into t_unit (UNITNAME) values ('z'); insert into t_unit (UNITNAME) values ('q'); insert into t_unit (UNITNAME) values ('1'); insert into t_unit (UNITNAME) values ('2'); insert into t_unit (UNITNAME) values ('3'); insert into t_unit (UNITNAME) values ('4'); commit; end; ---check data select * from t_unit; ---create pipe function , important step create or replace function autolinefeed return unit PIPELINED as temp_str varchar2(32767):=''; temp_index number:=0; temp_count number:=0; temp_sum number:=0; temp_unit1 varchar2(50); temp_unit2 varchar2(50); temp_unit3 varchar2(50); temp_unit4 varchar2(50); begin select count(*) into temp_sum from t_unit; if temp_sum<=0 then pipe row(null); return; end if; for v_unit in (select * from t_unit) loop temp_count:=(temp_count+1); if (mod(temp_count,4)=1) then temp_unit1 :=v_unit.unitname; elsif (mod(temp_count,4)=2) then temp_unit2 :=v_unit.unitname; elsif (mod(temp_count,4)=3) then temp_unit3 :=v_unit.unitname; else temp_unit4 :=v_unit.unitname; end if; if (mod(temp_count,4)=0) then pipe row(unit_type(temp_unit1,temp_unit2,temp_unit3,temp_unit4)); else if (temp_sum=temp_count) then if (mod(temp_count,4)=1) then pipe row(unit_type(temp_unit1,'','','')); elsif (mod(temp_count,4)=2) then pipe row(unit_type(temp_unit1,temp_unit2,'','')); elsif (mod(temp_count,4)=3) then pipe row(unit_type(temp_unit1,temp_unit2,temp_unit3,'')); else pipe row(unit_type(temp_unit1,temp_unit2,temp_unit3,temp_unit4)); end if; end if; end if; end loop; exception when others then pipe row(null); end; select * from table(autolinefeed);
oracle 动态列
原创
©著作权归作者所有:来自51CTO博客作者QUANWEIRU的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
SpringBoot 动态加载 jar 包,动态配置方案
SpringBoot 动态加载 jar 包,动态配置方案
java spring 加载 -
Oracle pivot 行转列、unpivo列转行函数使用详解
一 pivot函数表达式: 有一个职工部门工资表pivot_tmp,数据如下: 如果我们想要求各部门的工资和,很简
Oracle数据库 pivot行转列 unpivot行转列 字段 数据 -
Oracle 行转列pivot 、列转行unpivot 的Sql语句总结
Oracle 行转列、列转行 的Sql语句总结
oracle 行转列 列转行 wm_concat pivot -
oracle行转列(动态行转不定列)sql 存储过程 建表 测试数据 行转列