数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

Oracle中table函数的应用


发布日期:2023年10月23日
 
Oracle中table函数的应用

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

用游标传递数据

利用游标 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 (scalendar_month_desc ) YEAR untry_id country

SUM (sum_amount_sold) sum_amount_sold

FROM sum_sales_month_mv s customers c

WHERE scust_id = ccust_id

AND untry_id IN (US UK FR ES JP AU)

GROUP BY SUBSTR (scalendar_month_desc ) untry_id;

CREATE MATERIALIZED VIEW sum_es_gend_mv

BUILD DEFERRED

REFRESH FAST

ENABLE QUERY REWRITE

AS

SELECT SUBSTR (scalendar_month_desc ) YEAR

scalendar_month_desc cal_month ccust_gender

SUM (sum_amount_sold) sum_amount_sold

FROM sum_sales_month_mv s customer c

WHERE scust_id = ccust_id

AND untry_id = ES

AND sunstr (scalendar_month_desc ) =

GROUP BY SUBSTR (scalendar_month_desc )

scalendar_month_desc

ccust_gender;

定义对象类型和基于对象类型的表类型

定义对象类型并且为进一步引用做好准备

)定义对象类型TYPE sales_country_t

CREATE TYPE sales_country_t AS OBJECT (

YEAR VARCHAR ()

country CHAR ()

sum_amount_sold NUMBER

);

)定义表类型TYPE SUM_SALES_COUNTRY_T_TAB

CREATE TYPE sum_sales_country_t_tab AS TABLE OF sales_country_t;

)定义对象类型TYPE sales_gender_t

CREATE TYPE sales_gender_t AS OBJECT (

YEAR VARCHAR ()

country_id CHAR ()

cust_gender CHAR ()

sum_amount_sold NUMBER

);

)定义表类型TYPE SUM_SALES_GENDER_T_TAB

CREATE TYPE sum_sales_gender_t_tab AS TABLE OF sales_gender_t;

)定义对象类型TYPE sales_roll_t

CREATE TYPE sales_roll_t AS OBJECT (

channel_desc VARCHAR ()

country_id CHAR ()

sum_amount_sold NUMBER

);

)定义表类型TYPE SUM_SALES_ROLL_T_TAB

CREATE TYPE sum_sales_roll_t_tab AS TABLE OF sales_roll_t;

)检查一下建立的类型

SELECT object_name object_type status

FROM user_objects

WHERE object_type = TYPE;

定义包Create package and define REF CURSOR

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;

定义表函数

)定义表函数FUNCTION Table_Ref_Cur_Week

CREATE OR REPLACE FUNCTION table_ref_cur_week (cur CURSORrefcur_t)

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 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 + ;

objsetEXTEND;

objset (i) := sales_country_t (YEAR country sum_amount_sold);

END LOOP;

CLOSE cur;

RETURN objset;

END;

/

)定义表函数FUNCTION Table_Ref_Cur_Strong

CREATE OR REPLACE FUNCTION table_ref_cur_strong (cur cursor_pkgstrong_refcur_t)

RETURN sum_sales_country_t_tab PIPELINED

IS

YEAR VARCHAR ();

country CHAR ();

sum_amount_sold NUMBER;

i NUMBER := ;

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;

/

)定义表函数FUNCTION Table_Ref_Cur_row

CREATE OR REPLACE FUNCTION table_ref_cur_row (cur cursor_pkgrow_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_recYEAR := in_recYEAR;

untry := untry;

out_recsum_amount_sold := in_recsum_amount_sold;

PIPE ROW (out_rec);

END LOOP;

CLOSE cur;

RETURN;

END;

/

)定义表函数FUNCTION Gender_Table_Ref_Cur_Week

CREATE OR REPLACE FUNCTION gender_table_ref_cur_week (cur cursor_pkgrefcur_t)

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 country_id cust_gender sum_amount_sold;

EXIT WHEN cur%NOTFOUND; exit when last row is fetched

i := i + ;

objsetEXTEND;

objset (i) :=

sum_sales_gender_t (YEAR country_id cust_gender sum_amount_sold);

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 = AU)));

上一篇:OracleDML子句RETURNINGINTO

下一篇:Oracle快速删除数据字典管理的表空间