oracle pipelined返回值函数 针对数据汇总统计 返回结果集方法
/*打开日志输出*/
Set serveroutput on ;
/*创建类型*/
create or replace type type_flux_data_stat_o as object
(
ifinoctetsbps number ,
ifoutoctetsbps number ,
collecttime number
);
/*创建类型归属为表类型*/
create or replace type type_flux_data_stat as table of type_flux_data_stat_o;
/*pipelined创建函数 返回表类型*/
create or replace FUNCTION f_linkgroupstat(begin_time IN NUMBER,
end_time IN NUMBER,
lg_id in varchar2,
table_name varchar2 )
return type_flux_data_stat
pipelined as
/*游标申明*/
v_Cur SYS_REFCURSOR ;
/*sql临时变量*/
v_SQLStatement string (10000 );
/*表类型*/
v_Table type_flux_data_stat_o;
/*流入字节数临时变量*/
tmp_ifinoctetsbps NUMBER ;
/*流出字节数临时变量*/
tmp_ifoutoctetsbps NUMBER ;
/*流入字节数汇总*/
total_ifinoctetsbps NUMBER ;
/*流出字节数汇总*/
total_ifoutoctetsbps NUMBER ;
/*起始时间窗格*/
tmp_begin_time NUMBER ;
/*结束时间窗格*/
tmp_end_time NUMBER ;
begin
/*时间窗格偏移量为5分钟(300秒)*/
tmp_begin_time := begin_time;
tmp_end_time := begin_time + 300 ;
total_ifinoctetsbps := 0 ;
total_ifoutoctetsbps := 0 ;
loop
exit when tmp_begin_time > end_time;
v_SQLStatement := 'select sum(ifinoctetsbps) ifinoctetsbps,sum(ifoutoctetsbps) ifoutoctetsbps from ' ||
table_name ||
' a where exists (select 1 from tm_linkgroup_cportdirection b where a.getway = b.getway and a.port_info=b.ifindex_info and lg_id in (' ||
lg_id ||
') and a.device_id = b.device_id ) and a.collecttime >=' ||
tmp_begin_time || ' and a.collecttime <=' ||
tmp_end_time || ' order by collecttime' ;
Dbms_Output.put_line(v_SQLStatement);
/*针对字符串sql打开游标*/
open v_Cur for v_SQLStatement;
tmp_begin_time := tmp_begin_time + 300 ;
tmp_end_time := tmp_end_time + 300 ;
total_ifinoctetsbps := 0 ;
total_ifoutoctetsbps := 0 ;
loop
/*将游标的值放入零食变量中*/
fetch v_Cur
into tmp_ifinoctetsbps, tmp_ifoutoctetsbps;
/*当游标中不存在值时跳出游标*/
EXIT WHEN v_Cur% NOTFOUND;
total_ifinoctetsbps := total_ifinoctetsbps + tmp_ifinoctetsbps;
total_ifoutoctetsbps := total_ifoutoctetsbps + tmp_ifoutoctetsbps;
end loop ;
/*单行记录初始化*/
v_Table := type_flux_data_stat_o(total_ifinoctetsbps,
total_ifoutoctetsbps,
tmp_begin_time);
/*将记录压入至结果集中*/
pipe row (v_Table);
/*关闭游标*/
close v_Cur;
end loop ;
Exception
when others then
Dbms_Output.put_line( Sqlerrm );
end f_linkgroupstat;
select * from table(f_linkgroupstat(1361980800,1362067200,'34','FLUX_DATA_2013_2_28')) a; 289 rows selected. Elapsed: 00:00:00.28
执行时间为:28ms
oracle pipelined返回值函数 针对数据汇总统计 返回结果集方法的更多相关文章
- c语言进阶4-有返回值函数
一. 从函数返回 从函数返回就是返回语句的第一个主要用途.在程序中,有两种方法可以终止函数的执行,并返回到调用函数的位置.第一种方法是在函数体中,从第一句一直执行到最后一句,当所有语句 ...
- c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题
c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题 例如: string myFunc(){ theLogics(); } 发现调用: myFunc(); 崩溃. 但调用: cout ...
- go语言基础之有参有返回值函数的使用
1.有参有返回值函数的使用 示例1: package main //必须 import "fmt" //go官方推荐写法 func MaxAndMin(a, b int) (max ...
- Python函数01/函数的初识/函数的定义/函数调用/函数的返回值/函数的参数
Python函数01/函数的初识/函数的定义/函数调用/函数的返回值/函数的参数 内容大纲 1.函数的初识 2.函数的定义 3.函数的调用 4.函数的返回值 5.函数的参数 1.函数初识 # def ...
- c语言main函数返回值、参数详解(返回值是必须的,0表示正常退出)
C语言Main函数返回值 main函数的返回值,用于说明程序的退出状态.如果返回0,则代表程序正常退出:返回其它数字的含义则由系统决定.通常,返回非零代表程序异常退出. 很多人甚至市面上的一些书籍,都 ...
- SpringMVC入门(二)—— 参数的传递、Controller方法返回值、json数据交互、异常处理、图片上传、拦截器
一.参数的传递 1.简单的参数传递 /* @RequestParam用法:入参名字与方法名参数名不一致时使用{ * value:传入的参数名,required:是否必填,defaultValue:默认 ...
- C++获取Lua全局变量和执行Lua多参数多返回值函数
C++代码: // LuaAndC.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #i ...
- SpringMVC(二)返回值设置、数据在域中的保存与SpringMVC案例
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.返回值的设置 1.返回 String [1]返回 String 默认情况 @RequestMappi ...
- ASP.Net MVC 在ajax接收controller返回值为Json数据
首先,再次回忆一下ajax的标准用法:(这张图写的比较详细了)(转) 页面部分ajax代码: $.ajax({ url: "/Home/Login?account=&q ...
随机推荐
- hdu3415:最大k子段和,单调队列
题目大意:给定长度为n的数组,求出最大的区间和,其中区间长度在[1,k]之间 分析: 学动态规划的时候我们会遇到一个经典问题 最大子段和,这个题跟最大子段和很类似 不同的是区间的长度有限制,无法用原算 ...
- poj1284:欧拉函数+原根
何为原根?由费马小定理可知 如果a于p互质 则有a^(p-1)≡1(mod p)对于任意的a是不是一定要到p-1次幂才会出现上述情况呢?显然不是,当第一次出现a^k≡1(mod p)时, 记为ep(a ...
- [Node.js] Using ES6 and beyond with Node.js
If you're used to using all the latest ES6+ hotness on the front end via Babel, working in Node.js c ...
- 实现简单的django上传文件
本文用django实现上传文件并保存到指定路径下,没有使用forms和models,步骤如下: 1.在模板中使用form表单,因为这个表单使用于上传文件的,所以method属性必须设置为post,而且 ...
- 深入解析MySQL replication协议
Why 最开始的时候,go-mysql只是简单的抽象mixer的代码,提供一个基本的mysql driver以及proxy framework,但做到后面,笔者突然觉得,既然研究了这么久mysql c ...
- Memory Analyzer Blog
引用:http://memoryanalyzer.blogspot.jp/2008/05/automated-heap-dump-analysis-finding.html Dienstag, 27. ...
- 华为oj - 统计大写字母个数
练手而已. 给初学者参考 #include <stdio.h> #include <string.h> int main(void) { char string[200]={' ...
- [Database] Deadlock avoidance protocol
如何避免Deadlock,如果我们能提前知道各个Process对于资源的需求情况,我们就可以用Banker's algorithm (银行家算法) 来解决问题.可是这在现在中不好实现,因为很难提前知道 ...
- SQl 判断 表 视图 临时表等 是否存在
1.判断是否存在addOneArticle这个存储过程 if Exists(select name from sysobjects where NAME = 'addOneArticle' and t ...
- [Mugeda HTML5技术教程之16]案例分析:制作跨屏互动游戏
本节我们将要做一个跨屏互动应用的案例分析,该应用时给一家商场做活动使用的,是一个跨屏爱消除游戏.PC端页面显示在连接在PC的大屏幕上,参与活动的玩家可以用自己的手机扫描PC端页面上的二维码,连接成功后 ...