度量快速开发平台-专业、快速的软件定制快开平台

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 部件 流程 SQL
查看: 1262|回复: 2
打印 上一主题 下一主题

[分享] Oracle 中 table 函数的应用浅析

[复制链接]

328

主题

3738

帖子

8566

积分

作者

Rank: 7Rank: 7Rank: 7

积分
8566
QQ
跳转到指定楼层
楼主
发表于 2020-7-9 16:29:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
表函数可接受查询语句或游标作为输入参数,并可输出多行数据。这篇文章主要介绍了Oracle 中 table 函数的应用浅析,需要的朋友可以参考下。




表函数可接受查询语句或游标作为输入参数,并可输出多行数据。该函数可以平行执行,并可持续输出数据流,被称作管道式输出。应用表函数可将数据转换分阶段处理,并省去中间结果的存储和缓冲表。


1. 用游标传递数据
利用游标 REF CURSOR 可将数据集(多行记录)传递到PL/SQL函数:
1
2
3
SELECT *
FROM TABLE (myfunction (CURSOR (SELECT *
         FROM mytab)));




2. 利用两个实体化视图(或表)作为样板数据
1
2
3
4
5
6
7
8
9
10
11
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





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
1
CREATE TYPE sum_sales_country_t_tab AS TABLE OF sales_country_t;



(3)定义对象类型:TYPE sales_gender_t
1
2
3
4
5
6
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
1
CREATE TYPE sum_sales_gender_t_tab AS TABLE OF sales_gender_t;



(5)定义对象类型:TYPE sales_roll_t
1
2
3
4
5
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
1
CREATE TYPE sum_sales_roll_t_tab AS TABLE OF sales_roll_t;



(7)检查一下建立的类型
1
2
3
SELECT object_name, object_type, status
FROM user_objects
WHERE object_type = 'TYPE';



4. 定义包:Create package and define REF CURSOR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 查询语句调用已被定义的表函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
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')));



以上所述是小编给大家介绍的Oracle 中 table 函数的应用浅析,希望对大家有所帮助。


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

235

主题

2547

帖子

5835

积分

论坛元老

Rank: 8Rank: 8

积分
5835
沙发
发表于 2020-2-3 18:14:02 | 只看该作者
回复 支持 反对

使用道具 举报

141

主题

1551

帖子

3573

积分

论坛元老

Rank: 8Rank: 8

积分
3573
板凳
发表于 2020-4-9 17:59:21 | 只看该作者
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|重庆度量科技  本站关键词:快速开发平台

GMT+8, 2024-11-24 20:59 , Processed in 0.129687 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表