表函數可接受查詢語句或游標作為輸入參數
利用游標 REF CURSOR 可將數據集(多行記錄)傳遞到PL/SQL函數
SELECT *
FROM TABLE (myfunction (CURSOR (SELECT *
FROM mytab)));
CREATE MATERIALIZED VIEW sum_sales_country_mv
BUILD IMMEDIATE
REFRESH COMPLETE
ENABLE QUERY REWRITE
AS
SELECT SUBSTR (s
SUM (sum_amount_sold) sum_amount_sold
FROM sum_sales_month_mv s
WHERE s
AND untry_id IN (
GROUP BY SUBSTR (s
CREATE MATERIALIZED VIEW sum_es_gend_mv
BUILD DEFERRED
REFRESH FAST
ENABLE QUERY REWRITE
AS
SELECT SUBSTR (s
s
SUM (sum_amount_sold) sum_amount_sold
FROM sum_sales_month_mv s
WHERE s
AND untry_id =
AND sunstr (s
GROUP BY SUBSTR (s
s
c
定義對象類型並且為進一步引用做好准備
(
CREATE TYPE sales_country_t AS OBJECT (
YEAR VARCHAR
country CHAR (
sum_amount_sold NUMBER
);
(
CREATE TYPE sum_sales_country_t_tab AS TABLE OF sales_country_t;
(
CREATE TYPE sales_gender_t AS OBJECT (
YEAR VARCHAR
country_id CHAR (
cust_gender CHAR (
sum_amount_sold NUMBER
);
(
CREATE TYPE sum_sales_gender_t_tab AS TABLE OF sales_gender_t;
(
CREATE TYPE sales_roll_t AS OBJECT (
channel_desc VARCHAR
country_id CHAR (
sum_amount_sold NUMBER
);
(
CREATE TYPE sum_sales_roll_t_tab AS TABLE OF sales_roll_t;
(
SELECT object_name
FROM user_objects
WHERE object_type =
CREATE OR REPLACE PACKAGE cursor_pkg
IS
TYPE sales_country_t_rec IS RECORD (
YEAR VARCHAR (
country CHAR (
sum_amount_sold NUMBER
);
TYPE sales_gender_t_rec IS RECORD (
YEAR VARCHAR
country_id CHAR (
cust_gender CHAR (
sum_amount_sold NUMBER
);
TYPE sales_roll_t_rec IS RECORD (
channel_desc VARCHAR
country_id CHAR (
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;
(
CREATE OR REPLACE FUNCTION table_ref_cur_week (cur CURSOR
RETURN sum_sales_country_t_tab
IS
YEAR VARCHAR (
country CHAR (
sum_amount_sold NUMBER;
objset sum_sales_country_t_tab := sum_sales_country_t_tab ();
i NUMBER :=
BEGIN
LOOP
FETCH cur
INTO YEAR
EXIT WHEN cur%NOTFOUND;
i := i +
objset
objset (i) := sales_country_t (YEAR
END LOOP;
CLOSE cur;
RETURN objset;
END;
/
(
CREATE OR REPLACE FUNCTION table_ref_cur_strong (cur cursor_pkg
RETURN sum_sales_country_t_tab PIPELINED
IS
YEAR VARCHAR (
country CHAR (
sum_amount_sold NUMBER;
i NUMBER :=
BEGIN
LOOP
FETCH cur
INTO YEAR
EXIT WHEN cur%NOTFOUND;
PIPE ROW (sales_country_t (YEAR
END LOOP;
CLOSE cur;
RETURN;
END;
/
(
CREATE OR REPLACE FUNCTION table_ref_cur_row (cur cursor_pkg
RETURN sum_sales_country_t_tab PIPELINED
IS
in_rec cur%ROWTYPE;
out_rec sales_country_t := sales_country_t (NULL
BEGIN
LOOP
FETCH cur
INTO in_rec;
EXIT WHEN cur%NOTFOUND;
out_rec
untry := untry;
out_rec
PIPE ROW (out_rec);
END LOOP;
CLOSE cur;
RETURN;
END;
/
(
CREATE OR REPLACE FUNCTION gender_table_ref_cur_week (cur cursor_pkg
RETURN sum_sales_gender_t_tab
IS
YEAR VARCHAR
country_id CHAR (
cust_gender CHAR (
sum_amount_sold NUMBER;
objset sum_sales_gender_t_tab := sum_sales_gender_t_tab ();
i NUMBER :=
BEGIN
LOOP
FETCH cur
INTO YEAR
EXIT WHEN cur%NOTFOUND;
i := i +
objset
objset (i) :=
sum_sales_gender_t (YEAR
END LOOP;
CLOSE cur;
RETURN objset;
END;
/
下列 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 =
From:http://tw.wingwit.com/Article/program/Oracle/201311/18328.html