CREATE TYPE t_tf_obj AS OBJECT ( id NUMBER, name VARCHAR2(50) ); /
CREATE TYPE t_tf_type IS TABLE OF t_tf_obj; /
CREATE OR REPLACE FUNCTION get_tab (cnt IN NUMBER) RETURN t_tf_type AS l_tab t_tf_type := t_tf_type(); BEGIN FOR i IN 1 .. cnt LOOP l_tab.extend; l_tab(l_tab.last) := t_tf_obj(i, 'Name is ' || i); END LOOP;
RETURN l_tab; END; /
XB@ora12c> SELECT * FROM TABLE(get_tab(10));
ID NAME ---------- ------------------------- 1 Name is 1 2 Name is 2 3 Name is 3 4 Name is 4 5 Name is 5 6 Name is 6 7 Name is 7 8 Name is 8 9 Name is 9 10 Name is 10
10 rows selected.
如果版本是12.2及以上,可以连关键字TABLE都省掉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
XB@ora12c> SELECT * FROM get_tab(10);
ID NAME ---------- -------------------------------- 1 Name is 1 2 Name is 2 3 Name is 3 4 Name is 4 5 Name is 5 6 Name is 6 7 Name is 7 8 Name is 8 9 Name is 9 10 Name is 10
CREATE OR REPLACE FUNCTION get_tab_ptf (cnt IN NUMBER) RETURN t_tf_type PIPELINED AS BEGIN FOR i IN 1 .. cnt LOOP PIPE ROW(t_tf_obj(i, 'Name is ' || i)); END LOOP;
RETURN; END; /
XB@ora12c> SELECT * FROM get_tab_ptf(10);
ID NAME ---------- ------------------------------------------------------------------------------------------------------------------------------------------------------ 1 Name is 1 2 Name is 2 3 Name is 3 4 Name is 4 5 Name is 5 6 Name is 6 7 Name is 7 8 Name is 8 9 Name is 9 10 Name is 10
CREATE OR REPLACE FUNCTION get_tab_ptf (cnt IN NUMBER) RETURN t_tf_type PIPELINED AS BEGIN FOR i IN 1 .. cnt LOOP PIPE ROW(t_tf_obj(i, 'Name is ' || i)); END LOOP; RETURN; END; /
Function created.
XB@ora12c> XB@ora12c> SELECT * FROM get_tab_ptf(10) where rownum>=5;
ID NAME ---------- ------------------------------------------------------------------------------------------------------------------------------------------------------ 1 Name is 1 2 Name is 2 3 Name is 3 4 Name is 4 5 Name is 5
CREATE OR REPLACE FUNCTION get_tab_ptf (cnt IN NUMBER) RETURN t_tf_type PIPELINED AS BEGIN FOR i IN 1 .. cnt LOOP PIPE ROW(t_tf_obj(i, 'Name is ' || i)); END LOOP; RETURN; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.put_line('EXCEPTION'); RAISE; END; /
XB@ora12c> SELECT * FROM get_tab_ptf(10) where rownum>=5;
ID NAME ---------- ------------------------------------------------------------------------------------------------------------------------------------------------------ 1 Name is 1 2 Name is 2 3 Name is 3 4 Name is 4 5 Name is 5
CREATE OR REPLACE FUNCTION get_tab_ptf (cnt IN NUMBER) RETURN t_tf_type PIPELINED AS BEGIN FOR i IN 1 .. cnt LOOP PIPE ROW(t_tf_obj(i, 'Name is ' || i)); END LOOP; RETURN; EXCEPTION WHEN NO_DATA_NEEDED THEN DBMS_OUTPUT.put_line('NO_DATA_NEEDED'); RAISE; WHEN OTHERS THEN DBMS_OUTPUT.put_line('EXCEPTION'); RAISE; END; /
Function created.
XB@ora12c> XB@ora12c> SELECT * FROM get_tab_ptf(10) where rownum>=5;
ID NAME ---------- ------------------------------------------------------------------------------------------------------------------------------------------------------ 1 Name is 1 2 Name is 2 3 Name is 3 4 Name is 4 5 Name is 5
CREATE OR REPLACE FUNCTION get_stat RETURN NUMBER AS l_sum NUMBER; BEGIN SELECT a.value INTO l_sum FROM v$mystat a, v$statname b WHERE a.statistic# = b.statistic# AND b.name = 'session pga memory'; RETURN l_sum; END get_stat; /
declare v_begin number; v_end number; BEGIN v_begin:=sys.get_stat; FOR cur IN (SELECT * FROM TABLE(get_tab(10000))) LOOP NULL; END LOOP; v_end:=sys.get_stat; DBMS_OUTPUT.put_line('Begin memory is: '||v_begin); DBMS_OUTPUT.put_line('End memory is: '||v_end); DBMS_OUTPUT.put_line('Total memory used: '||(v_end-v_begin)); END; 16 / Begin memory is: 4753336 End memory is: 9013176 Total memory used: 4259840
set serveroutput on; declare v_begin number; v_end number; BEGIN v_begin:=sys.get_stat; FOR cur IN (SELECT * FROM TABLE(get_tab_ptf(10000))) LOOP NULL; END LOOP; v_end:=sys.get_stat; DBMS_OUTPUT.put_line('Begin memory is: '||v_begin); DBMS_OUTPUT.put_line('End memory is: '||v_end); DBMS_OUTPUT.put_line('Total memory used: '||(v_end-v_begin)); END; 16 / Begin memory is: 4032440 End memory is: 4032440 Total memory used: 0
XB@ora12c> SELECT * 2 FROM TABLE(get_tab_ptf(10));
ID NAME ---------- ------------------------------------------------------------------------------------------------------------------------------------------------------ 1 Name is 1 2 Name is 2 3 Name is 3 4 Name is 4 5 Name is 5 6 Name is 6 7 Name is 7 8 Name is 8 9 Name is 9 10 Name is 10
10 rows selected.
XB@ora12c> @x
PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SQL_ID 2gpbj20w5kmbn, child number 0 ------------------------------------- SELECT * FROM TABLE(get_tab_ptf(10))
XB@ora12c> select /*+ DYNAMIC_SAMPLING(2)*/ * FROM TABLE(get_tab_ptf(10));
ID NAME ---------- ------------------------------------------------------------------------------------------------------------------------------------------------------ 1 Name is 1 2 Name is 2 3 Name is 3 4 Name is 4 5 Name is 5 6 Name is 6 7 Name is 7 8 Name is 8 9 Name is 9 10 Name is 10
10 rows selected.
XB@ora12c> @x Display execution plan for last statement for this session from library cache...
PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SQL_ID 76sppwrcf832x, child number 0 ------------------------------------- select /*+ DYNAMIC_SAMPLING(2)*/ * FROM TABLE(get_tab_ptf(10))