ORACLE create a custom function return type varchar

Description of Requirement: two tables, the following need to query tmp1 table id_new name in tmp2 in
the SELECT from tmp1;
ORACLE create a custom function return type varchar
the SELECT
from tmp2;

ORACLE create a custom function return type varchar
method:
advantages: simple, direct sql show
bad place: If the primary table data is too big, ten hundreds of millions, then performance will be greatly decreased, the second method is recommended at this time
the SELECT a.id_old,
the TO_CHAR (wm_concat (DISTINCT a.id_new)) id_new,
the TO_CHAR (wm_concat (DISTINCT b.name)) name
from TMP2 b,
(the SELECT a.id_old, REGEXP_SUBSTR (a.id_new, '[^,] +',. 1, Level) id_new
from TMP1 A
Connect by Level <= regexp_count (a.id_new, ',') +. 1) A
WHERE a.id_new = b.id_old (+)
Group by a.id_old;

Method Two: Create custom function to achieve
Create or Replace function f_tmp_split (p_str VARCHAR2, P_F VARCHAR2)
return VARCHAR2 IS
V_POS PLS_INTEGER: = 0; - get the current position of the separator
v_pre_pos pls_integer: = 1; - starting from the first of several taken
v_len pls_integer: = 0; - length of the string
v_len1 pls_integer: = 0; - delimiter length
v_result dbms_sql.Varchar2_Table; - result set
v_num pls_integer: = 1; - the number of elements
v_name_class varchar2 (1000); - collection returned
v_name_tmp varchar2 (1000); - returns the value of splicing
the begin
v_len: = length (p_str);
v_len1: = length (P_F);
the while V_POS <v_len Loop
V_POS: = InStr (p_str, P_F, v_pre_pos);
IF 0 = the then V_POS
v_pre_pos: = v_len;
V_result (v_num): = substr (p_str, v_pre_pos);
the begin
SELECT a.name
into v_name_tmp
from tmp2 a
where a.id_old = v_result(v_num);
exception
when no_data_found then
v_name_tmp := '';
end;
v_name_class := v_name_class || v_name_tmp;
if v_pre_pos >= v_len then
exit;
end if;
else
v_result(v_num) := substr(p_str, v_pre_pos, v_pos - v_pre_pos);
begin
select a.name || p_f
into v_name_tmp
from tmp2 a
where a.id_old = v_result(v_num);
exception
when no_data_found then
v_name_tmp := '';
end;
v_name_class := v_name_class || v_name_tmp;
v_pre_pos := v_pos + v_len1;
end if;
end loop;
return v_name_class;
end;

Results are as follows:
ORACLE create a custom function return type varchar

ORACLE create a custom function return type varchar

Guess you like

Origin blog.51cto.com/12777507/2433789