zoukankan      html  css  js  c++  java
  • oracle函数返回表的写法

    从oracle 9i 开始,提供了一个叫做“管道化表函数”的概念,来解决这个问题
    这种类型的函数,必须返回一个集合类型,且标明 pipelined
    这个oracle函数不能返回具体变量,必须以一个空 return 返回
    这个oracle函数中,通过 pipe row () 语句来送出要返回的表中的每一行

    调用这个oracle函数的时候,通过 table() 关键字把管道流仿真为一个数据集

    具体写法如下:

    create type row_type2 as object(username1 varchar2(100),dw1 varchar2(100),ts number);  //创建行类型,同要返回的表的类型一致。
    create type table_type2 as table of row_type2;  
    create or replace function fun1(dateSJ varchar2) return table_type2
    pipelined as  v row_type2; 
    begin 
    for myrow in 
    (
        with a as
         (
             select (select username from users where userid= t.fromid) as username1,to_char((select dwbmmc from ks_xqpm_dwbm where dwbmid=(select deptid from users where userid=t.fromid) )) as dw1,count(*) as ts from mobile t where to_char(sendtime,'yyyy-mm')=dateSJ group by fromid
         )
         ,
         b as
         (
             select '合计' as  username1,'' as dw1,(select count(*)  from mobile  where to_char(sendtime,'yyyy-mm')=dateSJ) as ts from dual
         )
         select username1,dw1,ts  from a union all select username1,dw1,ts from b
    )
    loop  v := row_type2(myrow.username1, myrow.dw1,myrow.ts); 
    pipe row (v); 
    end loop; 
    return; 
    end;  

    调用方法: select * from table(fun1('2012-01'));

    注:

  • 相关阅读:
    第一次参赛经历:ecfinal总结
    滑雪(dp或记忆化搜索)
    dp入门题(数塔)
    SQL语句:子查询
    【原创】SQL语句原理解析
    gitignore规则探究
    路径分隔符:正斜线/、反斜线、双反斜线\的区别
    高并发系统设计方法
    js变量作用域,变量提升和函数传参
    数据库设计:三范式
  • 原文地址:https://www.cnblogs.com/BetterWF/p/2350505.html
Copyright © 2011-2022 走看看