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'));

    注:

  • 相关阅读:
    mongodb的aggregate聚合操作详解
    2013, Lost connection to MySQL server during query
    事务失败的重试策略
    mongodb的shell脚本
    mongodb的currentOp
    mongodb插入数据
    Too many threads are already waiting
    connection timed out
    python3-request.session 的使用
    intellij-永久破解最新版本idea 2020.3.1
  • 原文地址:https://www.cnblogs.com/BetterWF/p/2350505.html
Copyright © 2011-2022 走看看