Article Directory
1 Overview
- Function pipeline parallel execution , the function used in the normal
dbms_output
output information, the server needs to perform other functions to return to full-time to the client. If the client needs in real time some of the information during the execution of the output function, Oracle9i later, can function pipeline ( large data processing performance ). - Keyword
pipelined
indicates that this is a function oracle conduit, its return value type must be set in function,pipe row
the syntax is used to return the collection of individual elements , function to an emptyreturn
sentence ends, to indicate that it has finished.
2 Examples
2.1 First create a table type
-- 直接定义类型
CREATE OR REPLACE TYPE scott.ty_list IS TABLE OF VARCHAR2(300);
-- PL/SQL 中可在 package head 中定义
-- index by ... 是为了不再手动扩充 extend(1)
TYPE scott.ty_list IS TABLE OF VARCHAR2(300) INDEX BY PLS_INTEGER;
2.2 Real-time return data
ready:
1. arraysize 是 sql*plus 中可以设置的一个参数: 表示一次可以从数据库服务端获取的记录行数
2. 首先需要设置 arraysize 为 1,否则服务器会按照默认的 15 来向客户端返回信息,这会影响我们测试效果
3. 查询命令: show arraysize
4. 设置命令: set arraysize 15
Verification: PL/SQL code:
pipeline function:
CREATE OR REPLACE FUNCTION scott.f_pipelined_test RETURN scott.ty_list
PIPELINED IS
BEGIN
PIPE ROW('start...');
FOR i IN 1 .. 5 LOOP
PIPE ROW(i || ' ' || to_char(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'));
sys.dbms_lock.sleep(1); -- 等待 1 秒
END LOOP;
PIPE ROW('end...');
RETURN;
END;
/
General functions:
CREATE OR REPLACE FUNCTION scott.f_normal_test RETURN scott.ty_list IS
v_list_out_put scott.ty_list;
BEGIN
-- 初始化
v_list_out_put := scott.ty_list();
-- 扩展信息,可在 package 定义 type 时,增加 index by pls_integer 自动扩展 extend
FOR i IN 1 .. 5 LOOP
v_list_out_put.extend(1); -- 手动扩展
v_list_out_put(i) := i || ' ' ||
to_char(SYSDATE, 'YYYY-MM-DD HH24:MI:SS');
sys.dbms_lock.sleep(1); -- 等待 1 秒
END LOOP;
RETURN v_list_out_put;
END;
/
sql*plus window verification demo
hint:
sql*plus
It is to interact with the Oracle client tools (Oracle installed when customers bring their own end)plsql
Is a good visual development tool
in conclusion:
- Pipeline function: real-time output
- General function: output after the operation is completed
Dynamic icon:
3 Other examples
Serial number | description |
---|---|
1 | The pipeline function does not support debug |
2 | String cutting split |
I will use these for the time being, if there are any more, continue to add...
The pipeline function does not support debug:
Show results:
First define the type:
CREATE TYPE scott.ty_object_map IS OBJECT
(
v_key NUMBER,
v_value VARCHAR2(100)
)
;
/
CREATE TYPE scott.ty_table_map IS TABLE OF scott.ty_object_map;
/
-- 删除type、若需要
DROP TYPE scott.ty_table_map;
DROP TYPE scott.ty_object_map;
package head:
CREATE OR REPLACE PACKAGE scott.pkg_char_handle_util IS
FUNCTION f_split_string(p_string IN VARCHAR2,
p_delimiter IN VARCHAR2) RETURN scott.ty_table_map
PIPELINED;
END pkg_char_handle_util;
/
package body:
CREATE OR REPLACE PACKAGE BODY scott.pkg_char_handle_util IS
--************************************************
-- 功能: 切割字符串
-- 参数: p_string 字符串
-- p_delimiter 分隔符
-- 返回值: table 类型
--************************************************
FUNCTION f_split_string(p_string IN VARCHAR2,
p_delimiter IN VARCHAR2) RETURN scott.ty_table_map
PIPELINED IS
v_length PLS_INTEGER := length(p_string); -- 字符串长度
v_start PLS_INTEGER := 1; -- 字符开始位置
v_index PLS_INTEGER; -- 下标
v_key PLS_INTEGER := 1; -- 截取后,字符串的个数
v_object_map scott.ty_object_map;
BEGIN
WHILE (v_start <= v_length) LOOP
-- 获取符合条件的 第一个下标
v_index := instr(p_string, p_delimiter, v_start);
IF v_index = 0 THEN
-- 没有符合条件的记录,直接打印,如 string = 'a', 直接打印 a
v_object_map := scott.ty_object_map(v_key,
substr(p_string, v_start));
PIPE ROW(v_object_map);
RETURN;
ELSE
-- 否则,循环截取打印
v_object_map := scott.ty_object_map(v_key,
substr(p_string,
v_start,
v_index - v_start));
PIPE ROW(v_object_map);
-- 循环判断依据: 每次 '分隔符' 后 '第一个字符'
v_start := v_index + 1;
END IF;
-- 个数递增
v_key := v_key + 1;
END LOOP;
RETURN;
END f_split_string;
END pkg_char_handle_util;
/