知识大全 Oracle中table函数的应用
Posted 函数
篇首语:人格成熟的重要标志:宽容、忍让、和善。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Oracle中table函数的应用相关的知识,希望对你有一定的参考价值。
Oracle中table函数的应用 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
表函数可接受查询语句或游标作为输入参数 并可输出多行数据 该函数可以平行执行 并可持续输出数据流 被称作管道式输出 应用表函数可将数据转换分阶段处理 并省去中间结果的存储和缓冲表
用游标传递数据
利用游标 REF CURSOR 可将数据集(多行记录)传递到PL/SQL函数
SELECT *
FROM TABLE (myfunction (CURSOR (SELECT *
FROM mytab)));
利用两个实体化视图(或表)作为样板数据
CREATE MATERIALIZED VIEW sum_sales_country_mv
BUILD IMMEDIATE
REFRESH PLETE
ENABLE QUERY REWRITE
AS
SELECT SUBSTR (s calendar_month_desc ) YEAR untry_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 untry_id IN ( US UK FR ES JP AU )
GROUP BY SUBSTR (s calendar_month_desc ) untry_id;
CREATE MATERIALIZED VIEW sum_es_gend_mv
BUILD DEFERRED
REFRESH FAST
ENABLE QUERY REWRITE
AS
SELECT SUBSTR (s calendar_month_desc ) 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 untry_id = ES
AND sunstr (s calendar_month_desc ) =
GROUP BY SUBSTR (s calendar_month_desc )
s calendar_month_desc
c cust_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 CURSOR refcur_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 + ;
objset EXTEND;
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_pkg strong_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_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;
untry := untry;
out_rec sum_amount_sold := in_rec sum_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_pkg refcur_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 + ;
objset EXTEND;
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
cha138/Article/program/Oracle/201311/18328相关参考
知识大全 Oracle中sign函数和decode函数的使用
Oracle中sign函数和decode函数的使用 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
DUMP函数的输出格式类似: 类型<[长度]>符号/指数位[数字数字数字数字] 各位的含义如下: 类型:Number型Type=(类型代码可以从Oracle的文档上查到) 长度
DECODE函数相当于一条件语句(IF)它将输入数值与函数中的参数列表相比较根据输入值返回一个对应值函数的参数列表是由若干数值及其对应结果值组成的若干序偶形式当然如果未能与任何一个实参序偶匹配成功
许多软件公司都理解开发不依赖于特定数据库类型(例如OracleSQLServerDB)的应用程序的重要性它可以让客户们选择自己习惯的平台一般来说软件开发人员都能够识别出他们的负责数据库维护的客户和
创建一个get_name函数返回值是String类型(注这里的返回类型是可以根据自己的需要来设置)程序如下 createorreplacefunctionget_name(strinvarcha
Oracle中Decode()函数使用技巧 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! DEC
Inoracle/PLSQLthesubstrfunctionsallowsyoutoextractasubstringfromastring Thesyntaxforthesubstrfunc
oracle中SQL语句可以使用的函数 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 字符串处理
在Java中调用Oracle的过程和函数 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 内容或简
知识大全 SQL中Charindex和Oracle中对应的函数Instr对比
SQL中Charindex和Oracle中对应的函数Instr对比 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一