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 函数的应用浅析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数260
191.63 KB下载数245
143.91 KB下载数1139
183.71 KB下载数640
644.84 KB下载数2752
相关文章
发现更多好内容猜你喜欢
AI推送时光机Oracle 中 table 函数的应用浅析
数据库2024-04-02
Oracle中的table()函数使用
数据库2023-05-12
浅析React Hook中useEffecfa函数的使用
数据库2022-11-22
浅析php中函数调用函数的不同方式
数据库2023-05-14
浅析C++中的间接宏函数
数据库2024-04-02
深入浅析Node中的util.promisify()函数
数据库2023-05-14
Oracle Level函数应用实例分析
数据库2024-09-02
C++浅析内联函数的使用
数据库2024-04-02
C语言浅析函数的用法
数据库2024-04-02
Go语言中init函数与匿名函数使用浅析
数据库2023-01-16
Python浅析匿名函数lambda的用法
数据库2024-04-02
浅析JavaScript中的特殊函数及用法小结
数据库2024-04-02
浅析C语言中的setjmp与longjmp函数
数据库2022-11-15
浅析php函数中参数数量不同的情况
数据库2023-05-14
Oracle中的分析函数汇总
数据库2024-04-02
Oracle中的instr()函数应用及使用详解
数据库2024-04-02
深入浅析Vue3中的生命周期函数
数据库2023-05-14
浅析python中5个带key的内置函数
数据库2024-04-02
浅析JavaScript中的程序控制流和函数
数据库2022-11-22
Oracle DECODE函数实例分析与应用场景
数据库2024-03-08
咦!没有更多了?去看看其它编程学习网 内容吧