CREATE OR REPLACE TYPE node_parent_type AS OBJECT ( lvl NUMBER, node VARCHAR2(200), PARENT VARCHAR2(200) )
/
CREATE OR REPLACE TYPE str2Branch_Type AS TABLE OF node_parent_type
/
CREATE OR REPLACE FUNCTION Str2Branch ( p_string IN VARCHAR2 , p_delim IN VARCHAR2 )
RETURN str2Branch_Type
PIPELINED
AS
l_str LONG;
l_piece LONG;
l_parent LONG := NULL;
l_n NUMBER;
l_lvl NUMBER := 0;
BEGIN
l_str := p_string;
LOOP l_n := INSTR(l_str, p_delim);
IF (l_n = 1) --Starts with a delimiter
THEN l_n := INSTR(SUBSTR(l_str,2), p_delim);
END IF;
EXIT WHEN (NVL(l_n,0) = 0);
l_piece := SUBSTR( l_str, 1, l_n ); --First piece
l_str := SUBSTR( l_str, l_n+1 ); --Rest
l_n := INSTR( l_piece, p_delim, -1 );
EXIT
WHEN (NVL(l_n,0) = 0); --Only needed, if we have to end with a delimiter.
pipe ROW( node_parent_type ( l_lvl, LTRIM(RTRIM(SUBSTR(l_piece,l_n+1))), l_parent ) );
l_parent := LTRIM(RTRIM(SUBSTR(l_piece,l_n+1)));
l_lvl := l_lvl + 1;
END LOOP;
RETURN;
END;
/
Wednesday, August 23, 2006
Forgot the all important str2branch function
This function was inspired by the str2table function, which can be found on asktom.oracle.com.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment