oracle tabs作用,Oracle 中 table 函数的应用浅析
表函数可接受查询语句或游标作为输入参数,并可输出多行数据。该函数可以平行执行,并可持续输出数据流,被称作管道式输出。应用表函数可将数据转换分阶段处理,并省去中间结果的存储和缓冲表。
1. 用游标传递数据
利用游标 REF CURSOR 可将数据集(多行记录)传递到PL/SQL函数:
SELECT *
FROM TABLE (myfunction (CURSOR (SELECT *
FROM mytab)));
2. 利用两个实体化视图(或表)作为样板数据
CREATE MATERIALIZED VIEW sum_sales_country_mv
BUILD IMMEDIATE
REFRESH COMPLETE
ENABLE QUERY REWRITE
AS
SELECT SUBSTR (s.calendar_month_desc, 1, 4) YEAR, c.country_id country,
SUM (sum_amount_sold) sum_amount_sold
FROM sum_sales_month_mv s, customers c
WHERE s.cust_id = c.cust_id
AND c.country_id IN ('US', 'UK', 'FR', 'ES', 'JP', 'AU')
GROUP BY SUBSTR (s.calendar_month_desc, 1, 4), c.country_id
CREATE MATERIALIZED VIEW sum_es_gend_mv
BUILD DEFERRED
REFRESH FAST
ENABLE QUERY REWRITE
AS
SELECT SUBSTR (s.calendar_month_desc, 1, 4) YEAR,
s.calendar_month_desc cal_month, c.cust_gender,
SUM (sum_amount_sold) sum_amount_sold
FROM sum_sales_month_mv s, customer c
WHERE s.cust_id = c.cust_id
AND c.country_id = 'ES'
AND sunstr (s.calendar_month_desc, 1, 4) = '2000'
GROUP BY SUBSTR (s.calendar_month_desc, 1, 4),
s.calendar_month_desc,
c.cust_gender;
3. 定义对象类型和基于对象类型的表类型
定义对象类型并且为进一步引用做好淮备。
(1)定义对象类型:TYPE sales_country_t
CREATE MATERIALIZED VIEW sum_es_gend_mv
BUILD DEFERRED
REFRESH FAST
ENABLE QUERY REWRITE
AS
SELECT SUBSTR (s.calendar_month_desc, 1, 4) YEAR,
s.calendar_month_desc cal_month, c.cust_gender,
SUM (sum_amount_sold) sum_amount_sold
FROM sum_sales_month_mv s, customer c
WHERE s.cust_id = c.cust_id
AND c.country_id = 'ES'
AND sunstr (s.calendar_month_desc, 1, 4) = '2000'
GROUP BY SUBSTR (s.calendar_month_desc, 1, 4),
s.calendar_month_desc,
c.cust_gender;
(2)定义表类型:TYPE SUM_SALES_COUNTRY_T_TAB
CREATE TYPE sum_sales_country_t_tab AS TABLE OF sales_country_t;
(3)定义对象类型:TYPE sales_gender_t
CREATE TYPE sales_gender_t AS OBJECT (
YEAR VARCHAR2 (4),
country_id CHAR (2),
cust_gender CHAR (1),
sum_amount_sold NUMBER
);
(4)定义表类型:TYPE SUM_SALES_GENDER_T_TAB
CREATE TYPE sum_sales_gender_t_tab AS TABLE OF sales_gender_t;
(5)定义对象类型:TYPE sales_roll_t
CREATE TYPE sales_roll_t AS OBJECT (
channel_desc VARCHAR2 (20),
country_id CHAR (2),
sum_amount_sold NUMBER
);
(6)定义表类型:TYPE SUM_SALES_ROLL_T_TAB
CREATE TYPE sum_sales_roll_t_tab AS TABLE OF sales_roll_t;
(7)检查一下建立的类型
SELECT object_name, object_type, status
FROM user_objects
WHERE object_type = 'TYPE';
4. 定义包:Create package and define REF CURSOR
CREATE OR REPLACE PACKAGE cursor_pkg
I TYPE sales_country_t_rec IS RECORD (
YEAR VARCHAR (4),
country CHAR (2),
sum_amount_sold NUMBER
);
TYPE sales_gender_t_rec IS RECORD (
YEAR VARCHAR2 (4),
country_id CHAR (2),
cust_gender CHAR (1),
sum_amount_sold NUMBER
);
TYPE sales_roll_t_rec IS RECORD (
channel_desc VARCHAR2 (20),
country_id CHAR (2),
sum_amount_sold NUMBER
);
TYPE sales_country_t_rectab IS TABLE OF sales_country_t_rec;
TYPE sales_roll_t_rectab IS TABLE OF sales_roll_t_rec;
TYPE strong_refcur_t IS REF CURSOR
RETURN sales_country_t_rec;
TYPE row_refcur_t IS REF CURSOR
RETURN sum_sales_country_mv%ROWTYPE;
TYPE roll_refcur_t IS REF CURSOR
RETURN sales_roll_t_rec;
TYPE refcur_t IS REF CURSOR;
END corsor_pkg;
5. 定义表函数
(1)定义表函数:FUNCTION Table_Ref_Cur_Week
CREATE OR REPLACE FUNCTION table_ref_cur_week (cur CURSOR.refcur_t)
RETURN sum_sales_country_t_tab
IS
YEAR VARCHAR (4);
country CHAR (2);
sum_amount_sold NUMBER;
objset sum_sales_country_t_tab := sum_sales_country_t_tab ();
i NUMBER := 0;
BEGIN
LOOP
-- Fetch from cursor variable
FETCH cur
INTO YEAR, country, sum_amount_sold;
EXIT WHEN cur%NOTFOUND;
-- exit when last row is fetched
-- append to collection
i := i + 1;
objset.EXTEND;
objset (i) := sales_country_t (YEAR, country, sum_amount_sold);
END LOOP;
CLOSE cur;
RETURN objset;
END;
/
(2)定义表函数:FUNCTION Table_Ref_Cur_Strong
CREATE OR REPLACE FUNCTION table_ref_cur_strong (cur cursor_pkg.strong_refcur_t)
RETURN sum_sales_country_t_tab PIPELINED
IS
YEAR VARCHAR (4);
country CHAR (2);
sum_amount_sold NUMBER;
i NUMBER := 0;
BEGIN
LOOP
FETCH cur
INTO YEAR, country, sum_amount_sold;
EXIT WHEN cur%NOTFOUND; -- exit when last row fetched
PIPE ROW (sales_country_t (YEAR, country, sum_amount_sold));
END LOOP;
CLOSE cur;
RETURN;
END;
/
(3)定义表函数:FUNCTION Table_Ref_Cur_row
CREATE OR REPLACE FUNCTION table_ref_cur_row (cur cursor_pkg.row_refcur_t)
RETURN sum_sales_country_t_tab PIPELINED
IS
in_rec cur%ROWTYPE;
out_rec sales_country_t := sales_country_t (NULL, NULL, NULL);
BEGIN
LOOP
FETCH cur
INTO in_rec;
EXIT WHEN cur%NOTFOUND; -- exit when last row is fetched
out_rec.YEAR := in_rec.YEAR;
out_rec.country := in_rec.country;
out_rec.sum_amount_sold := in_rec.sum_amount_sold;
PIPE ROW (out_rec);
END LOOP;
CLOSE cur;
RETURN;
END;
/
(4)定义表函数:FUNCTION Gender_Table_Ref_Cur_Week
CREATE OR REPLACE FUNCTION gender_table_ref_cur_week (cur cursor_pkg.refcur_t)
RETURN sum_sales_gender_t_tab
IS
YEAR VARCHAR2 (4);
country_id CHAR (2);
cust_gender CHAR (1);
sum_amount_sold NUMBER;
objset sum_sales_gender_t_tab := sum_sales_gender_t_tab ();
i NUMBER := 0;
BEGIN
LOOP
FETCH cur
INTO YEAR, country_id, cust_gender, sum_amount_sold;
EXIT WHEN cur%NOTFOUND; -- exit when last row is fetched
i := i + 1;
objset.EXTEND;
objset (i) :=
sum_sales_gender_t (YEAR, country_id, cust_gender, sum_amount_sold);
END LOOP;
CLOSE cur;
RETURN objset;
END;
/
6. 调用表函数
下列 SQL 查询语句调用已被定义的表函数。
SELECT *
FROM TABLE (table_ref_cur_week (CURSOR (SELECT *
FROM sum_sales_country_mv)));
SELECT *
FROM TABLE (table_ref_cur_strong (CURSOR (SELECT *
FROM sum_sales_country_mv)));
SELECT *
FROM TABLE (table_ref_cur_row (CURSOR (SELECT *
FROM sum_sales_country_mv)));
SELECT *
FROM TABLE (table_ref_cur_week (CURSOR (SELECT *
FROM sum_sales_country_mv
WHERE country = 'AU')));
以上所述是aspphp.online小编给大家介绍的Oracle 中 table 函数的应用浅析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31481770/viewspace-2143430/,如需转载,请注明出处,否则将追究法律责任。
oracle tabs作用,Oracle 中 table 函数的应用浅析相关推荐
- python中int函数的用法浅析_Python中int()函数的用法浅析
int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int) Help on class int in module __builtin__ ...
- python中int用法,Python中int()函数的用法浅析
int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int) Help on class int in module __builtin__ ...
- 关于oracle中table函数的使用
使用oracle的table函数,查询函数返回的结果集,就如同查询普通表一样查询返回的结果集. 说明: 1.表函数可接受查询语句或游标作为输入参数,并可输出多行数据. 2.该函数可以平行执行,并可持 ...
- oracle mysql substr_Oracle数据库中substr()函数简介说明
摘要: 下文讲述Oracle数据库中substr函数应用简介,如下所示: oracle substr函数功能说明 substr函数功能: substr函数在oracle数据库中的功能为:字符串截取函数 ...
- lua中table函数库
一部分的table函数只对其数组部分产生影响, 而另一部分则对整个table均产生影响. 下面会分开说明. table.concat(table, sep, start, end) concat是c ...
- python中int函数是什么作用_python中int函数怎么用
python中int函数怎么用,字符串,函数,数字,出现在,赋值 python中int函数怎么用 易采站长站,站长之家为您整理了python中int函数怎么用的相关内容. int() 函数用于将一个字 ...
- C语言beep中有什么作用,C++中发声函数Beep用法
当我们听到电脑上出现美妙的旋律的时候,是不是发现这是一个很伟大的开发,这就是C语言中Beep,下面爱站技术频道小编就给大家介绍C++中发声函数Beep用法. 下面是正文: •其实啊,Windows A ...
- python中format函数作用_python中format函数什么意思
python中format函数什么意思? Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前的 % ...
- python中tracer函数意思_浅析Python编写函数装饰器
本节主要介绍编写函数装饰器的相关内容. 跟踪调用 如下代码定义并应用一个函数装饰器,来统计对装饰的函数的调用次数,并且针对每一次调用打印跟踪信息. 这是一个通过类装饰的语法写成的装饰器,测试如下: 运 ...
最新文章
- ABAP:关于文本(Read_text,Save_text)
- php文件上传错误代码
- 聊一聊Load Average
- 飞畅科技教你如何选择合适的PoE交换机?
- java模块化按需加载,JavaScript模块化之使用requireJS按需加载
- 【初体验】valgrind分析程序性能
- FFT(快速傅里叶) c语言版
- 类扩展(Class Extension)
- 力扣-509 裴波那契数
- laravel 自带验证
- Python env
- Dreamoon Likes Coloring
- ThuThesis模板中\footnote每页重新从2编号的问题解决
- IDEA设置多行标签页
- Linux系统命令(电子邮件新闻组)
- ARX中非模态对话框
- 《微机原理第五版》期末知识总结(第五章---第七章)
- 机器人电焊电流电压怎么调_焊接机器人焊机电流电压匹配及行走速度调节(一)...
- 如何使用Categories
- 晶振、时钟周期、机器周期
热门文章
- 使用git提交项目到码云
- matlab swt函数,matlab swt 函数出错
- 2. with check option能起什么作用?_家装要选择第三方装修监理吗?为什么?
- 设计模式-中介者模式(21)
- JS获取当前时间date()的用法
- Nginx实现404页面的几种方法
- Apache基金会接收阿里RocketMQ为孵化项目
- ASP.NET MVC中使用DropDownList
- C Primer Plus 第8章 字符输入/输出和输入确认 8.1 单字符I/O
- 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)